我使用 Collections.sort()
按运行时间顺序对 MP3 列表进行排序,如果运行时间相等,则按标题按字母顺序排序,如果标题相等则按 Composer 。我将输入放入 stdin
即
输入示例
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
But then it doesn't come up with anything via the input. I am confused since it should work perfectly. I don't get any errors.
public class Lab3Algorithm {
public static void main(String args[]) throws IOException {
List<Song> songs = new ArrayList<Song>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numOfSongsRequired = Integer.parseInt(br.readLine());
String sep = br.readLine();
String line;
while((line = br.readLine()) != null) {
String[] fields = sep.split(line);
songs.add(new Song(fields[0], fields[1], fields[2]));
}
Collections.sort(songs);
System.out.println(songs);
}
}
public class Song implements Comparable<Song> {
private final String title;
private final String composer;
private final int runningTime;
public Song(String title, String composer, String runningTime) {
this.title = title;
this.composer = composer;
this.runningTime = Integer.parseInt(runningTime);
}
public String getTitle(){
return this.title;
}
public String getComposer(){
return this.composer;
}
public int getRunningTime(){
return this.runningTime;
}
@Override
public int compareTo(Song s) {
if (runningTime > s.runningTime) {
return 1;
} else if (runningTime < s.runningTime) {
return -1;
}
int lastCmp = title.compareTo(s.title);
return (lastCmp != 0 ? lastCmp : composer.compareTo(s.composer));
}
}
如果有人能指出我正确的方向,我会感激不尽。
最佳答案
String[] fields = sep.split(line);
似乎是错误的 - 您没有尝试拆分歌曲输入上的分隔符字符串;您想要在分隔符上分割输入的歌曲:
String[] fields = line.split(sep);
关于java - 使用 Collections.sort() 对 MP3 列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6162822/