Collections.sort(List list)
但要保证你的Vector中存储的对象,按照你的要求重写了equals()方法和compareTo()方法
若是没有,则用重写之,或者使用:
Collections.sort(List list, Comparator c)
但要保证你的Vector中存储的对象,按照你的要求重写了equals()方法和compareTo()方法
若是没有,则用重写之,或者使用:
Collections.sort(List list, Comparator c)
Object[] temp =list.toArray();
Arrays.sort(temp);
list = Arrays.asList(temp);
Arrays.sort(temp);
ListIterator i = list.listIterator();
for (int j = 0; j < temp.length; j++) {
i.next();
i.set(temp[j]);
}
这是Collections.sort(List list)的源码
Object a[] = list.toArray();
Arrays.sort(a);
ListIterator i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set(a[j]);
}
能不能按照点里面这个方法sum()的返回值排序?可不可以用这个方法Collections.sort(List,Comparator)?
这个方法该怎么使用?参数一定要List吗?Vector行么?Comparator该怎么定义?
Collections(points,points.element.sum())
~~~~~~~~~~~~~~~~~~~~~~~~
这后面该怎么写呢?
int thisVal = this.value;
int anotherVal = anotherInteger.value;
return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
}Vector是List接口的一个实现建议你还是好好看看书
他有两个方法:
(1)int compare(Object o1, Object o2): 对两个对象o1和o2进行比较,如果o1位于o2的前面,则返回负值,如果在排序顺序中认为o1和o2是相同的,返回0,如果o1位于o2的后面,则返回正值『与Comparable相似,0返回值不表示元素相等。一个0返回值只是表示两个对象排在同一位置。由Comparator用户决定如何处理。如果两个不相等的元素比较的结果为零,您首先应该确信那就是您要的结果,然后记录行为。』(2)boolean equals(Object obj): 指示对象obj是否和比较器相等。『该方法覆写Object的equals()方法,检查的是Comparator实现的等同性,不是处于比较状态下的对象。』
谢谢!!!!!
另外可以推荐一下讲java类库的书么?
int i;
int j;
public CompType(int n1, int n2) {
i = n1;
j = n2;
}
public String toString() {
return "[i = " + i + ", j = " + j + "]";
}
public int compareTo(Object rv) {
int rvi = ((CompType)rv).i + ((CompType)rv).j;
return ((i+j) < rvi ? -1 : ((i+j) == rvi ? 0 : 1));
}
public static void main(String[] args) {
CompType[] a = new CompType[]{new CompType(1,2),new CompType(2,3),new CompType(1,3),new CompType(5,4)};
Arrays.sort(a);
System.out.println(Arrays.asList(a));
}
}