2 ssf0531 ssf0531 于 2017.09.02 23:50 提问

Java字符串排序,小白求助~
import java.io.*;
 import java.util.*;
 import java.util.Comparator;
 public class Main {
    @SuppressWarnings("rawtypes")
    public static void main(String[] args) throws Exception {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        ArrayList songList = new ArrayList(n);
        String str = cin.nextLine();
        while (str != null) {
            str = cin.nextLine();
            if (str.length() == 1 || str.length() == 0)
                continue;
            songInfo a = new songInfo(str);
            songList.add(a);
        }
        cin.close();
        Collections.sort(songList, new sort());
        System.out.println(songList);
    }
 }
 class songInfo {
    public String title;
    public String composer;
    public int runningTime;
    public songInfo(String str) {
        String[] song = str.split("&");
        this.title = song[0];
        this.composer = song[1];
        this.runningTime = Integer.parseInt(song[2], 10);
        ;
    }
    public String getTitle() {
        return title;
    }
    public String getComposer() {
        return composer;
    }
    public int getRunningTime() {
        return runningTime;
    }
    public String toString() {
        return this.title + this.composer + this.runningTime;
    }
  }
 class sort implements Comparator {
    @Override
    public int compare(Object o1, Object o2) {
        songInfo s1 = (songInfo) o1;
        songInfo s2 = (songInfo) o2;
        if (s1.getRunningTime() < s2.getRunningTime())
            return 1;
        return -1;
    }
  }

/*
输入样本:
3
&
Pink Frost&Phillipps, Martin&234933
Se quel guerrier io fossi&Puccini, Giacomo&297539
Non piu andrai&Mozart&234933
M'appari tutt'amor&Flotow, F&252905
输出:
Se quel guerrier io fossi&Puccini, Giacomo&297539
M'appari tutt'amor&Flotow, F&252905
Non piu andrai&Mozart&234933
*/

输入第一行为输出的数量,第二行为分隔符,第三行开始是字符串由&分隔为三部分。
根据数字部分排序,遇到相同的根据第一部分字母排序。

才自学java三个星期,写了几个小时就写出来上面那一片,现在没有头绪了。
能不能请帮忙改到有正确的输出?
直接加我微信shishunfan2 也行,改完发20红包感谢啊

1个回答

Small_Mouse0
Small_Mouse0   Ds   Rxr 2017.09.03 12:00
已采纳

不知道题主解决了没,,,,还有红包吗,(●'◡'●),,,代码奉上,,随意大赏

 import java.io.*;
import java.util.*;
import java.util.Comparator;

public class demo {
    public static void main(String[] args) throws Exception {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        ArrayList<SongInfo> songList = new ArrayList<SongInfo>(n);
        cin.nextLine();//吃掉回车
        String split = cin.nextLine();
        String str;
        for (int i = 0; i < n; i++) {
            str = cin.nextLine();
            if (str.length() == 1 || str.length() == 0)
                continue;
            SongInfo a = new SongInfo(str, split);
            songList.add(a);

        }
        cin.close();
        Collections.sort(songList);
        for (SongInfo song : songList) {
            System.out.println(song);
        }
    }
}

class SongInfo implements Comparable {
    public String title;
    public String composer;
    public int runningTime;

    public SongInfo(String str, String spilitStr) {
        String[] song = str.split(spilitStr);
        this.title = song[0];
        this.composer = song[1];
        this.runningTime = Integer.parseInt(song[2], 10);
    }

    public String getTitle() {
        return title;
    }

    public String getComposer() {
        return composer;
    }

    public int getRunningTime() {
        return runningTime;
    }

    public String toString() {
        return this.title + this.composer + this.runningTime;
    }

    /**
     * 比较两个字符串
     *
     * @param str1
     * @param str2
     * @return
     */
    private int compareStr(String str1, String str2) {
        if (str1.equals(str2)) return 0;
        char[] str1Chars = str1.toCharArray();
        char[] str2Chars = str2.toCharArray();
        int less = str1Chars.length > str2Chars.length ? str1Chars.length : str2Chars.length;//取最小
        for (int i = 0; i < less; i++) {
            if (str1Chars[i] == str2Chars[i]) continue;
            if (str1Chars[i] > str2Chars[i]) {
                return 1;
            } else {
                return -1;
            }
        }
        return 0;
    }

    @Override
    public int compareTo(Object o) {
        SongInfo s1 = this;
        SongInfo s2 = (SongInfo) o;
        if (s1.getRunningTime() > s2.getRunningTime()) return -1;
        if (s1.getRunningTime() < s2.getRunningTime()) return 1;
        return compareStr(s1.getTitle(), s2.getTitle());
    }
}

qq_39396275
qq_39396275 回复小鼠标丶: 我竟然不知道以前关注过你了,
3 个月之前 回复
qq_39396275
qq_39396275 回复小鼠标丶: while (cin.hasNextLine()) { String str = cin.nextLine(); if (str.length() == 1 || str.length() == 0) continue; SongInfo a = new SongInfo(str, split); songList.add(a); } 你看着这样行不
3 个月之前 回复
Small_Mouse0
Small_Mouse0 回复嫌疑-人: 这个是题主写的,,改的时候一开始题主用的while,这个if可以排除空格,我就保留着,,最后看见前面已经输入个数了,于是改成for循环了,结果忘删了,,,,这句代码改成这样能好点if (str.length() == 0) { i--; continue; },排除只输入回车的情况
3 个月之前 回复
qq_39396275
qq_39396275 if (str.length() == 1 || str.length() == 0) continue;这代码什么意思?看不出来是判断什么的?
3 个月之前 回复
Small_Mouse0
Small_Mouse0 回复嫌疑-人: 哪里,,,我很水的,,,就是吹牛逼比较厉害(●'◡'●)
3 个月之前 回复
qq_39396275
qq_39396275 小鼠标,你好厉害
3 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片