gpt4 book ai didi

java - 如何按 boolean 字段对 ArrayList 对象进行排序

转载 作者:太空宇宙 更新时间:2023-11-04 07:17:12 25 4
gpt4 key购买 nike

嗨,我有一个自定义对象的 ArrayList。我可以使用下面的代码按 int 和 String 字段对它们进行排序:

public class ArtistNameComparator implements Comparator<MP3Track>{

@Override
public int compare(MP3Track t1, MP3Track t2) {
return t1.getArtist().compareTo(t2.getArtist());
}
}

public class TrackNoComparator implements Comparator<MP3Track>{

@Override
public int compare(MP3Track t1, MP3Track t2) {
try{
if(t1.getTrackNo() > t2.getTrackNo())
return 1;
else if(t1.getTrackNo() < t2.getTrackNo())
return -1;
else
return 0;
}//Debug this possibility
catch(NullPointerException e){
throw new NullPointerException(e + "...compare int comparator!.");
}
}
}

但是如果我尝试使用以下任一示例按 boolean 字段对列表进行排序:

public class FavoriteComparator implements Comparator<MP3Track>{

@Override
public int compare(MP3Track t1, MP3Track t2) {
boolean v1 = t1.getFav();
boolean v2 = t2.getFav();
return Boolean.compare( v1, v2 );
}

@Override
public int compare(MP3Track t1, MP3Track t2) {
boolean v1 = t1.getFav();
boolean v2 = t2.getFav();
if( v1 && ! v2 ) {
return +1;
}
if( ! v1 && v2 ) {
return -1;
}
return 0;
}
}

我收到以下消息:

Exception in thread "main" java.lang.RuntimeException: 
Uncompilable source code - Erroneous sym type: java.util.Collections.sort
at mp3_box.MP3Catalogue.sortTracks(MP3Catalogue.java:88)
at mp3_box.MP3Player.main(MP3Player.java:56) Java Result: 1

我是 Java 新手,如果有人能解释为什么这是错误的,我将不胜感激?

提前非常感谢..

从这里调用:

public void sortTracks(){
String choice = null;
System.out.println(face.getSortCatalogueMenu());
System.out.print("ENTER->");
try{
choice = (keyboard.readString().trim().toLowerCase());
}
catch(InvalidUserInputException e){
System.err.println(e + "Error in sortTracks");
}
switch (choice) {
case "n":
Collections.sort(vector, new TrackNoComparator());
System.out.println("Your MP3 Catalogue has been sorted by Track Number");
break;
case "t":
Collections.sort(vector, new TrackNameComparator());
System.out.println("Your MP3 Catalogue has bee sorted by Track Name");
break;
case "a":
Collections.sort(vector, new ArtistNameComparator());
System.out.println("Your MP3 Catalogue has been sorted by Artist Name");
break;
case "b":
Collections.sort(vector, new AlbumNameComparator());
System.out.println("Your MP3 Catalogue has been sorted by Album Name");
break;
case "f":
Collections.sort(vector, new FavoriteComparator());
break;
case "x":
return;
}
for (MP3Track t : vector)
System.out.println(t);
}//End sortTrack

这是从 main 调用的:

    String choice = "";
while(!(choice.equals("e"))){
System.out.println(face.getMainMenu());
System.out.print("ENTER->");
try{
choice = (keyboard.readString().trim().toLowerCase());
}
catch(InvalidUserInputException e){
System.out.println("InvalidUsedInputException thrown " + e.getMessage());
}
switch (choice){
case "h":
MP3Cat.shuffleTracks();
break;
case "d":
MP3Cat.deleteTrack();
break;
case "a":
MP3Cat.addTrack();
break;
case "w":
MP3Cat.swapTrack();
break;
case "s":
MP3Cat.catSearch();
break;
case "i":
MP3Cat.trackInfo();
break;
case "t":
MP3Cat.sortTracks();
break;
case "r":
MP3Cat.exploreTracks();
break;
case "f":
MP3Cat.favorites();<---------
case "x":
System.exit(1);
}
}

最佳答案

不可能在一个类中定义具有相同签名的两个方法,但有重复的public int Compare(MP3Track t1, MP3Track t2)

关于java - 如何按 boolean 字段对 ArrayList 对象进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19799052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com