使用 Collections.sort() 对 MP3 列表进行排序
我使用 Collections.sort()
按运行时间顺序对 MP3 列表进行排序,如果运行时间相等,则按标题的字母顺序排序,如果标题相等,则按作曲家排序。我将输入放入 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
但随后它不会通过输入产生任何内容。我很困惑,因为它应该完美地工作。我没有收到任何错误。
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));
}
}
如果有人能给我指出正确的方向,那会让我感激不已。
I'm using Collections.sort()
to sort a list of MP3s in order of runtime, and if runtime is equal, then sort alphabetically by title, and if title is equal then sort by composer. I put the input into stdin
i.e.
Example Input
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));
}
}
If someone could point me in the right direction, that would make me grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
String[] fields = sep.split(line);
似乎是错误的 - 您没有尝试拆分歌曲输入上的分隔符字符串;您想在分隔符上分割输入的歌曲:String[] fields = sep.split(line);
seems wrong - you're not trying to split the separator string on the song input; you want to split the song input on the separator:你的 while 是错误的
readline 仅在存在 EOF 时给出 null (ctrl-z 或 ctrl-d 取决于平台),
否则它只会阻止等待下一行
your while is faulty
readline only gives null on if there is a EOF (ctrl-z or ctrl-d depending on the platform)
otherwise it just blocks waiting on the next line