当前位置: 代码迷 >> J2SE >> Comparator接口的compare方法如何实现排序的?
  详细解决方案

Comparator接口的compare方法如何实现排序的?

热度:203   发布时间:2016-04-24 01:39:27.0
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)等等
------解决方案--------------------
这个一个匿名的内部类,它把所有的操作都封装了。你如果想知道,建议你去它里面的原码吧。
  相关解决方案