在JDK 1.6的源代码中,Collections类的swap方法如下所示:
public static void swap(List<?> list, int i, int j) {
final List l = list;
l.set(i, l.set(j, l.get(i)));
}
Run Code Online (Sandbox Code Playgroud)
创建传递列表的最终副本的原因是什么?他们为什么不直接修改传递的列表?在这种情况下,您还会获得原始类型警告.
nos*_*sid 18
没有列表的副本,只有列表的引用副本.最终关键字并不重要.但是,使用原始类型很重要.如果使用该参数,编译器将报告错误:
public static void swap(List<?> list, int i, int j) {
// ERROR: The method set(int, capture#3-of ?) in the type List<capture#3-of ?>
// is not applicable for the arguments (int, capture#4-of ?)
list.set(i, list.set(j, list.get(i)));
}
Run Code Online (Sandbox Code Playgroud)
这意味着,他们正在使用中间变量来规避泛型的缺点,并摆脱错误信息.
有趣的问题是:他们为什么不使用通用方法?以下代码有效:
public static <T> void swap(List<T> list, int i, int j) {
list.set(i, list.set(j, list.get(i)));
}
Run Code Online (Sandbox Code Playgroud)
答案是,这个方法在使用原始类型调用方法的旧代码中产生警告:
List list = ...;
// WARNING: Type safety: Unchecked invocation swap2(List, int, int)
// of the generic method swap2(List<T>, int, int) of type Swap
Collections.swap(list, 0, 1);
Run Code Online (Sandbox Code Playgroud)