繁体   English   中英

ArrayList使用Comparator对对象进行排序

[英]ArrayList sorting objects using Comparator

我在尝试按重量对这些Pet对象进行排序时遇到错误。 我确信这很简单,但我不明白为什么这个比较不起作用。

错误:返回类型与java.util.Comparator.compare(Pet,Pet)不兼容

ArrayListNoDups类

import java.util.*;
import java.util.Collections;
import java.util.Comparator;
import java.lang.Object;

public class ArrayListNoDups {
    public static void main(String[] args) {
        ArrayList<Pet> list = new ArrayList<Pet>();
        String name;
        Integer age;
        Double weight;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("If you wish to stop adding Pets to the list, please put a 0 for all 3 fields.");

        do {
            System.out.println("Enter a String for Pet name: ");
            name = keyboard.next();
            System.out.println("Enter an int for Pet age: ");
            age = keyboard.nextInt();
            System.out.println("Enter a double for Pet weight: ");
            weight = keyboard.nextDouble();

            if (name.length() > 0 && age > 0 && weight > 0)
                list.add(new Pet(name, age, weight));
        } while (name.length() > 0 && age > 0 && weight > 0);

        System.out.println("Your list sorted by WEIGHT ========================= ");
        Collections.sort(list, Pet.SortByWeight);
        for (Pet p2 : list)
            p2.writeOutput();

    }
}

宠物类

import java.util.*;

public class Pet {
    private String name;
    private Integer age; // in years
    private double weight; // in pounds

    public void writeOutput() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age + " years");
        System.out.println("Weight: " + weight + " pounds");
    }

    public void set(String newName) {
        name = newName;
        // age and weight are unchanged.
    }

    public void set(int newAge) {
        if (newAge <= 0) {
            System.out.println("Error: illegal age.");
            System.exit(0);
        } else
            age = newAge;
        // name and weight are unchanged.
    }

    public void set(double newWeight) {
        if (newWeight <= 0) {
            System.out.println("Error: illegal weight.");
            System.exit(0);
        } else
            weight = newWeight;
        // name and age are unchanged.
    }

    public Pet(String name, int age, double weight) {
        this.name = name;
        this.age = age;
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public double getWeight() {
        return weight;
    }

    public static Comparator<Pet> SortByWeight = new Comparator<Pet>() {
        public double compare(Pet pet1, Pet pet2) {
            return pet1.getWeight() - pet2.getWeight();
        }
    };
}

Comparator接口的compare方法返回一个int ,而不是double

更改:

public double compare(Pet pet1, Pet pet2)

至:

public int compare(Pet pet1, Pet pet2)

比较器可以如下所示:

public static Comparator<Pet> SortByWeight = new Comparator<Pet>() {
    public int compare(Pet pet1, Pet pet2) {
        return (int)(pet1.getWeight() - pet2.getWeight());
    }
};

是问题比较需要返回一个整数遵守该Comparator接口。

您可以在比较中转换为int,或者更好,只需使用Double.compare()

旁注:在这些情况下使用@Override注释(实现通用接口)将使编译器帮助您更早地看到这些问题。

试试这个 :

public int compare(Pet pet1, Pet pet2) {
            return new Double(pet1.getWeight()).compareTo(new Double(pet2.getWeight()));
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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