BT的#java泛型#,我只想创建个泛型实例!
悲惨的是就连ArrayList<T>里边的elementData都是Object[]...不是T[]...
更悲惨的是,Collections.sort<T>处的T还是extends Comparable呢
等到实现代码里头调用到Arrays.sort的时候,传进去的就只是Object[]了
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]);
}
}
Arrays.sort实现代码又强制类型转换,把Object转换成了Comparable
public static void sort(Object[] a) {
Object[] aux = (Object[])a.clone();
mergeSort(aux, a, 0, a.length, 0);
}
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;
}
...
}
悲催的JDK不知道在玩什么