简体   繁体   中英

Sorting with comparable (Java)

I've implemented the interface comparable and the method compareTo(). I have a list named randomHumans that contains 10 objects. 5 objects with three fields: name, age and year they started studying, and 5 objects with two fields: name and age. I would like to sort my list, and tried using:

Collections.sort(randomHumans);

This gave me the following error message:

The method sort(List<T>) in the type Collections is not applicable for the arguments (ArrayList<Object>)

I then tried this code:

Collections.sort((List<T>) randomObjects);

But it just gives me two new error messages. Maybe I need to specify what field it should sort after, but I can't find how to implement this. Any help would be greatly appreciated.

main method:

public static void main (String[] args) {
    ArrayList<Object> randomObjects = new ArrayList<Object>();
    for (int j=0; j<5; j++) {
        Fysiker randomFysiker = new Fysiker();
        randomObjects.add(randomFysiker);

        Human randomHuman = new Human();
        randomObjects.add(randomHuman);
    }
    System.out.println(randomObjects.toString());

    //Collections.sort(randomObjects);
}

Human class:

class Human implements Comparable<Human> {

    int age;
    String name;

    public Human (int myAge, String myName) {
        name = myName;
        age = myAge;

    }

    public Human() {
        this(randomAge(),randomName());

    }

    public int compareTo(Human o) {
        return this.age - o.age;

    }

    protected static int randomAge() {
        Random randomGenerator = new Random();

        return randomGenerator.nextInt(100);

    }

    protected static String randomName() {
        Random randomGenerator = new Random();

        return "Name"+randomGenerator.nextInt(15);

    }

    public int getAge(){
        return age;
    }

    public String getName() {
        return name;
    }

    public String toString() {
        return "\nName: " + name + "\nAge: " + age + " yrs old\n";
    }


}

Fysiker class:

public class Fysiker extends Human {

    int year;

    public Fysiker(int myAge2, String myName2, int myYear) {

        name = myName2;
        year = myYear+1932;

        if (myAge2 >= 15+(2017-myYear)) {
            age = myAge2;   
        } else {
            age = 15+(2017-year);
        }
    }

    public Fysiker() {
        this(randomAge(),randomName(), randomYear());
    }

    protected static int randomYear() {
        Random randomGenerator = new Random();

        return randomGenerator.nextInt(83);
    }

    public int getYear(){
        return year;
    }

    public String toString() {
        return "\nName: " + name + "\nAge: " + age + " yrs old" + "\nStarted Physics: " + year+"\n";
    }
}

Just change the generic parameter from Object to Human

public static void main (String[] args) {

    List<Human> randomObjects = new ArrayList<>();
    for (int j = 0; j < 5; j++) {

        Fysiker randomFysiker = new Fysiker();
        randomObjects.add(randomFysiker);

        Human randomHuman = new Human();
        randomObjects.add(randomHuman);
    }
    System.out.println(randomObjects);

    Collections.sort(randomObjects);
}

When you write Collections.sort(randomHumans); randomHumans must be a List of Comparable. If you are 'forced' to use a List of Object, you must give a Comparator to explain how to compare each object :

Collections.sort(randomHumans, humanComparator);

It's all explained in the offcial documentation :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM