2015-04-27 210 views
5

當我嘗試按重量對這些Pet對象進行排序時,我正在收到錯誤。我相信這很簡單,但我不明白爲什麼這個比較不起作用。ArrayList使用比較器排序對象

Error: The return type is incompatible with 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(); 

    } 
} 

寵物類方法的Comparator

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

回答

7

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()); 
    } 
}; 
+1

@MartDellon只投的差異爲int - '回報(INT)(pet1.getWeight() - pet2.getWeight());' – Eran

+0

能見度,這個工程就像以及Ankit的答案。我剛剛嘗試了他的第一個。謝謝你們的幫助! – JoshTheGray

+1

@MartDellon請注意,如果您使用Ankit的解決方案,每次比較都將涉及創建兩個Double對象,這些對象將立即符合垃圾回收的條件。 n元素的體面排序算法需要n * log(n)比較,這意味着您將創建2 * n * log(n)雙目標併爲垃圾收集器提供額外的工作。這可能會使排序比我的解決方案慢,它對基元類型起作用,並且不會創建任何對象。 – Eran

0

的問題是compare需要返回一個整數,以符合該Comparator接口。

您可以在比較中將其轉換爲int,或者更好的方法是使用Double.compare()

備註:在這些情況下使用@Override註釋(實現通用接口)將使編譯器幫助您更早地看到這些問題。

0

試試這個:

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

這工作很好。謝謝! – JoshTheGray

+0

如果解決了您的問題,您可以接受答案。 :) –