Collections類
1.extends
X是X的子類
2.super
X是X的父類java
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) {spa
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,編譯器報錯。code
代碼
package sort;接口
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;ip
public class ArrayListTest {編譯器
public static void main(String[] args) { List<Student> a = new ArrayList<Student>(); Collections.sort(a); }
}it