泛型
Collections类
1.extends
X是X的子类
2.super
X是X的父类
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]);
}
}
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]);
}
}
复制代码
测试类
Student没有实现比较接口Comparable,编译器报错。
代码
package sort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ArrayListTest {
public static void main(String[] args) {
List<Student> a = new ArrayList<Student>();
Collections.sort(a);
}
}
复制代码