13

在 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)));
}

创建通过列表的最终副本的原因是什么?他们为什么不直接修改传递的列表?在这种情况下,您还会收到原始类型警告。

4

1 回答 1

19

没有列表的副本,只有对列表的引用的副本。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);
于 2012-05-28T17:29:26.707 回答