所以我正在研究一个比较器问题,但我不明白为什么Array.sort
第一堂课给了我以下错误:
Arrays 类型中的方法 sort(T[], Comparator) 不适用于参数 (ArrayList, CalorieComparator)
餐厅类:
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
public class Restaurant {
private ArrayList<Edible> elist;
public Restaurant() {
}
public void addEdibleItem(Edible item){
elist.add(item);
}
public List<Edible> orderByCalories(){
Arrays.sort(elist, new CalorieComparator());
}
卡路里比较器类:
import java.util.Comparator;
public class CalorieComparator implements Comparator {
public int compare(Object o1, Object o2){
Edible thisfood = (Edible)o1;
Edible otherfood = (Edible)o2;
if(thisfood.getCalories() > otherfood.getCalories())
return 1;
else if (thisfood.getCalories() < otherfood.getCalories())
return -1;
else
return 0;
}
}