日期:2014-05-20  浏览次数:20696 次

Comparator接口的compare方法怎么实现排序的????
new Comparator<Student>(){
public int compare(Student o1, Student o2) {
return o1.store - o2.store;
}
};

------解决方案--------------------
把对象保存在数组中,通过Arrays.sort来排序,会自动调用该方法
或者保存在List中,然后通过Collections.sort来排序,会自动调用该方法
或者保存在某些带有排序功能的集合,如TreeSet等等,也会自动调用该方法
如果是自己用代码实现,在for循环里自己调用 if (student1.compare(student1, student2) > 0)等等
------解决方案--------------------
这个一个匿名的内部类,它把所有的操作都封装了。你如果想知道,建议你去它里面的原码吧。