在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)));
}
创建传递列表的最终副本的原因是什么?他们为什么不直接修改传递的列表呢?在这种情况下,您还会收到原始类型警告。
最佳答案
没有列表的副本,只有对列表的引用的副本。 final 关键字并不重要。但是,使用原始类型很重要。如果改用参数,编译器会报错:
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)));
}
这意味着,他们正在使用中间变量来规避泛型的缺点,并摆脱错误消息。
有趣的问题是:他们为什么不使用泛型方法?以下代码有效:
public static <T> void swap(List<T> list, int i, int j) {
list.set(i, list.set(j, list.get(i)));
}
答案是,此方法会在使用原始类型调用该方法的旧代码中产生警告:
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);
关于java - 为什么 Collections.swap 将目标列表分配给原始类型的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10788259/