ssf0531
SS帆
2017-09-02 15:50

Java字符串排序,小白求助~

30
  • java
  • sort
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条回答

为你推荐