1

嗨,我有一个自定义对象的 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!.");
        }
    }
}

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

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

这就是从这里的主要调用:

    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);
        }
    }
4

1 回答 1

1

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

于 2013-11-05T21:15:19.220 回答