关于Collections.sort(list)方法 10
我明白在Student的class中implementCmparable是为了要自己定义compareTo的方法,但是在主方法中调用Collections.sort(al...
我明白在Student的class中implement Cmparable是为了要自己定义
compareTo的方法,但是在主方法中调用Collections.sort(al)为什么
就可以调用定义的compareTo方法了,这两个是怎么挂上钩的?看起来没什么关系啊。。~?
一直看不懂,谢谢!
代码如下:
import java.util.*;
public class Main{
public static void main(String args[]){
ArrayList al=new ArrayList();
al.add(new Student(2,"aa"));
al.add(new Student(1,"bb"));
al.add(new Student(3,"dd"));
al.add(new Student(3,"cc"));
Collections.sort(al);
Iterator it=al.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
class Student implements Comparable{
int id;
String name;
Student(int id,String name){
this.id=id;
this.name=name;
}
public int compareTo(Object o){
Student s=(Student)o;
int result=(id>s.id)?1:((id==s.id)?0:-1);
if(0==result){
result=name.compareTo(s.name);
}
return result;
}
public String toString(){
return "id="+this.id+",name="+this.name;
}
} 展开
compareTo的方法,但是在主方法中调用Collections.sort(al)为什么
就可以调用定义的compareTo方法了,这两个是怎么挂上钩的?看起来没什么关系啊。。~?
一直看不懂,谢谢!
代码如下:
import java.util.*;
public class Main{
public static void main(String args[]){
ArrayList al=new ArrayList();
al.add(new Student(2,"aa"));
al.add(new Student(1,"bb"));
al.add(new Student(3,"dd"));
al.add(new Student(3,"cc"));
Collections.sort(al);
Iterator it=al.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
class Student implements Comparable{
int id;
String name;
Student(int id,String name){
this.id=id;
this.name=name;
}
public int compareTo(Object o){
Student s=(Student)o;
int result=(id>s.id)?1:((id==s.id)?0:-1);
if(0==result){
result=name.compareTo(s.name);
}
return result;
}
public String toString(){
return "id="+this.id+",name="+this.name;
}
} 展开
2个回答
展开全部
可以看看源码,Collections的sort方法如下:
public static <T extends Comparable<? super T>> void sort(List<T> list) {
Object[] a = list.toArray();
Arrays.sort(a);
ListIterator<T> i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set((T)a[j]);
}
}
可以看到这里实际调用了Arrsys类的sort方法;我们再看看这个方法:
public static void sort(Object[] a) {
Object[] aux = (Object[])a.clone();
mergeSort(aux, a, 0, a.length, 0);
}
这个很简单,调用了另外一个方法mergeSort:
private static void mergeSort(Object[] src,
Object[] dest,
int low,
int high,
int off) {
int length = high - low;
// Insertion sort on smallest arrays
if (length < INSERTIONSORT_THRESHOLD) {
for (int i=low; i<high; i++)
for (int j=i; j>low &&
((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
swap(dest, j, j-1);
return;
}
// Recursively sort halves of dest into src
int destLow = low;
int destHigh = high;
low += off;
high += off;
int mid = (low + high) >>> 1;
mergeSort(dest, src, low, mid, -off);
mergeSort(dest, src, mid, high, -off);
// If list is already sorted, just copy from src to dest. This is an
// optimization that results in faster sorts for nearly ordered lists.
if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
System.arraycopy(src, low, dest, destLow, length);
return;
}
// Merge sorted halves (now in src) into dest
for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
dest[i] = src[p++];
else
dest[i] = src[q++];
}
}
看到没!!中间把Object对象强转成了Comparable,并直接调用了compareTo方法!!明白了吧!!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询