难以对链表进行排序

difficulty with sorting linked lists

有人可以向我解释如何使用 Collections.sort() 按姓氏对列表进行排序并显示排序后的列表。然后编写第二个内部 class 实现 Comparator 并覆盖 compare() 方法,允许对 size 实例变量进行比较。

 public Animal(){
        fname = "unknown";
        lname = "unknown";
        size = 0;
    }

    public Animal(String fname, String lname, int size) {
       this.fname = fname;
       this.lname = lname;
       this.size = size;
    }

   //getters and setters
    public String getFName(){
        return fname;
    }
    public void setFName(String fname){
        this.fname = fname;
    }

    public String getLName(){
        return lname;
    }
    public void setLName(String lname){
        this.lname = lname;
    }

    public int getSize(){
        return size;
    }
    public void setSize(int size){
        this.size = size;
    }

    //compare by fname
    public int compareTo(Animal lname) {
        return fname.compareTo(lname.fname);
    }

    //sound method
    abstract String sound();



    //toString method
    @Override
    public String toString(){
        return getFName() + " " + getLName() + " " + sound() + " " + getSize();
    }



}//end class

.

public class AnimalTester {

    //inner class implementing comparable
    static abstract class InnerClass implements Comparable<Animal>{  

            private static final String lname = "Not Given";

            List<Animal> dogList2 = new LinkedList<Animal>();
            List<Animal> catList2 = new LinkedList<Animal>();

            //override compareto and compare lname
            int dogListCompare = InnerClass.lname.compareTo(((Animal)    dogList2).getLName());
            int catListCompare = InnerClass.lname.compareTo(((Animal) catList2).getLName());

            }//end inner class

    public static void main(String[] args){
        String fileName = "animal.dat";
        Scanner input = new Scanner(System.in);     


        //create LinkedList
        List<Dog> dogList = new LinkedList<Dog>();
        List<Cat> catList = new LinkedList<Cat>();
        List<Dog> dogList2 = new LinkedList<Dog>();
        List<Cat> catList2 = new LinkedList<Cat>();

        String fname;
        String lname;

        //String yes = "y";
        char yes = 'y';
        int x = 0;

        //while(yes.equals("y")){
        while(yes == 'y' || yes == 'Y'){

            //declare & create a Dog & Cat object
            dogList.add(new Dog());
            System.out.print("First Name (dog): ");
            fname = input.next();
            System.out.print("Last Name (dog): ");
            lname = input.next();
            dogList2.add(new Dog());
            System.out.print("First Name Again (dog): ");
            fname = input.next();
            System.out.print("Last Name Again (dog): ");
            lname = input.next();

            System.out.println(" "); //line break  

            catList.add(new Cat());
            System.out.print("First Name (cat): ");
            fname = input.next();
            System.out.print("Last Name (cat): ");
            lname = input.next();
            catList2.add(new Cat());
            System.out.print("First Name Again (cat): ");
            fname = input.next();
            System.out.print("Last Name Again (cat): ");
            lname = input.next();

            dogList.get(x).setFName(fname);
            dogList.get(x).setLName(lname);
            catList.get(x).setFName(fname);
            catList.get(x).setLName(lname);
            dogList2.get(x).setFName(fname);
            dogList2.get(x).setLName(lname);
            catList2.get(x).setFName(fname);
            catList2.get(x).setLName(lname);

            x++; //increment x

            System.out.print("Continue? [y/n]: ");
            System.out.println(" ");
            yes = input.next().charAt(0);

        }//end while

        System.out.println("");
        //enhanced for loops
        for(Dog element:dogList){
            System.out.println(element.toString());   
        } 
        for(Dog element:dogList2){
            System.out.println(element.toString());   
        } 
        for(Cat element:catList2){
            System.out.println(element.toString());   
        }
        for(Cat element:catList){
            System.out.println(element.toString());   
        }

        //sort lists by fname
        Collections.sort(dogList, new Comparator<Animal>() {
            public int compare(Animal one, Animal fname) {
                return one.getFName().compareTo(fname.getLName());
            }
        }); 
        Collections.sort(catList, new Comparator<Animal>() {
            public int compare(Animal one, Animal fname) {
                return one.getFName().compareTo(fname.getLName());
            }
        }); 

        //sort lists by lname
        Collections.sort(dogList2, new Comparator<Animal>() {
            public int compare(Animal two, Animal lname) {
                return two.getLName().compareTo(lname.getFName());
            }
        }); 
        Collections.sort(catList2, new Comparator<Animal>() {
            public int compare(Animal two, Animal lname) {
                return two.getLName().compareTo(lname.getFName());
           }
       }); 

        //display lists
        System.out.println(" ");
        System.out.println("Initial List ON FName: "+dogList);
        Collections.sort(dogList);
        System.out.println("Sorted List ON FName: "+dogList);
        System.out.println(" ");
        System.out.println("Initial List ON FName: "+catList);
        Collections.sort(catList);
        System.out.println("Sorted List ON FName: "+catList);

        System.out.println(" ");
        System.out.println("Initial List ON LName: "+dogList2);
        Collections.sort(dogList2);
        System.out.println("Sorted List ON LName: "+dogList2);
        System.out.println(" ");
        System.out.println("Initial List ON LName: "+catList2);
        Collections.sort(catList2);
        System.out.println("Sorted List ON LName: "+catList2);



    }//end main 
}//end class

我需要 3 组列表,按名字、名字和大小排序。我已经用名字和名字尝试了所有方法,但我做不到...

一个快速的建议,而不是 InnerClass 实施 Comparable<Animal>,您希望它实施 Comparator<Animal>Comparable<T>表示这个对象可以和一个T进行比较,Comparator<T>表示这个对象可以比较两个T

查一查Oracle's tutorial on Object Ordering,其实很有参考价值。

您最好使用 comparator 对具有不同条件的相同 bean 进行排序。

这是一个示例代码:

Dog.class:

public class Dog extends Animal {

    public Dog(String fname, String lname, int size) {
        super(fname, lname, size);
    }

    @Override
    String sound() {
        return "bark";
    }

}

猫class:

public class Cat extends Animal {

    public Cat(String fname, String lname, int size) {
        super(fname, lname, size);
    }

    @Override
    String sound() {
        return "meauvvv";
    }

}

Animal.class:

public abstract class Animal {

    private String fname;

    private String lname;

    private int size;

    public Animal(String fname, String lname, int size) {
       this.fname = fname;
       this.lname = lname;
       this.size = size;
    }

   //getters and setters
    public String getFName(){
        return fname;
    }
    public void setFName(String fname){
        this.fname = fname;
    }

    public String getLName(){
        return lname;
    }
    public void setLName(String lname){
        this.lname = lname;
    }

    public int getSize(){
        return size;
    }
    public void setSize(int size){
        this.size = size;
    }

    //compare by fname
    public int compareTo(Animal lname) {
        return fname.compareTo(lname.fname);
    }

    //sound method
    abstract String sound();



    //toString method
    @Override
    public String toString(){
        return getFName() + " " + getLName() + " " + sound() + " " + getSize();
    }



}

DogCatComparator.class:

public class DogCatComparator implements Comparator<Animal> {

    @Override
    public int compare(Animal arg0, Animal arg1) {


        //null checks here

        if(arg0.sound().equals(arg1.sound())){
            return 0;
        }

        if(arg0.sound().equals("bark") && arg1.sound().equals("meauvvv")){
            return -1;
        }

        return 1;
    }

}

NameComparator.class:

public class NameComparator implements Comparator<Animal> {

    @Override
    public int compare(Animal arg0, Animal arg1) {

        //null check here
        return arg0.getFName().compareTo(arg1.getFName());
    }

}

SizeComparator.class:

public class SizeComparator implements Comparator<Animal> {

    @Override
    public int compare(Animal arg0, Animal arg1) {

        //null check here

        if(arg0.getSize() == arg1.getSize()){
            return 0;
        }

        return arg0.getSize() > arg1.getSize() ? 1 : -1;
    }

}

示例:

public class Main {



    public static void main(String[] args) {

        Dog dog1 = new Dog("dog1Name", "dog1lname", 5);
        Dog dog2 = new Dog("dog2Name", "dog2lname", 3);
        Cat cat1 = new Cat("cat1Name", "cat1lname", 7);
        Cat cat2 = new Cat("cat2Name", "cat2lname", 1);

        List<Animal> animals = new LinkedList<>();
        animals.add(cat2);
        animals.add(cat1);
        animals.add(dog2);
        animals.add(dog1);

        Collections.sort(animals, new DogCatComparator());
        printList(animals);

        Collections.sort(animals, new SizeComparator());
        printList(animals);

        Collections.sort(animals, new NameComparator());
        printList(animals);
    }


    private static void printList(List<Animal> animals){
        System.out.println("-------------");
        for(Animal animal: animals){
            System.out.println(animal.toString());
        }
    }

}

结果:

-------------
dog2Name dog2lname bark 3
dog1Name dog1lname bark 5
cat2Name cat2lname meauvvv 1
cat1Name cat1lname meauvvv 7
-------------
cat2Name cat2lname meauvvv 1
dog2Name dog2lname bark 3
dog1Name dog1lname bark 5
cat1Name cat1lname meauvvv 7
-------------
cat1Name cat1lname meauvvv 7
cat2Name cat2lname meauvvv 1
dog1Name dog1lname bark 5
dog2Name dog2lname bark 3