在源代码中java.util.Collection有一个函数调用shuffle:
@SuppressWarnings({"rawtypes", "unchecked"})
public static void shuffle(List<?> list, Random rnd) {
int size = list.size();
if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
for (int i=size; i>1; i--)
swap(list, i-1, rnd.nextInt(i));
} else {
Object arr[] = list.toArray();
// Shuffle array
for (int i=size; i>1; i--)
swap(arr, i-1, rnd.nextInt(i));
// Dump array back into list
// instead of using a raw type here, it's possible to capture
// the wildcard but it will require a call to a supplementary
// private method
ListIterator it = list.listIterator();
for (int i=0; i<arr.length; i++) {
it.next();
it.set(arr[i]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码中的注释说,“这里不是使用原始类型,而是可以捕获通配符,但它需要调用补充私有方法。”
这意味着什么?没有原始类型怎么写?
Oracle Docs 中有一个页面解释了“捕获通配符”的含义。
在 的情况下shuffle,您将“转储数组”操作提取到通用辅助方法中:
private static <T> void dumpArray(Object[] arr, List<T> list) {
ListIterator<T> it = list.listIterator();
for (int i=0; i<arr.length; i++) {
it.next();
it.set((T)arr[i]);
}
}
//...
dumpArray(arr, list);
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为正如文档所说:
多亏了 helper 方法,编译器在调用中使用推理来确定 T 是 CAP#1,捕获变量。