9

由于各种原因,我想将列表转换为数组,但是 Collection 包含本身是泛型的对象。

我已经尝试了以下四个选项来编译它而不需要@supressWarnings('unchecked')注释,但它们都不起作用。是否有解决方案可以使其正常工作,还是我被迫使用注释?

Iterator<T>[] iterators;
final Collection<Iterator<T>> initIterators = new ArrayList<Iterator<T>>();

// Type safety: Unchecked cast from Iterator[] to Iterator<T>[]
iterators = initIterators.<Iterator<T>>toArray(
        (Iterator<T>[])new Iterator[initIterators.size()]);

// Type safety: Unchecked invocation toArray(Iterator[]) of the generic 
// method toArray(T[]) of type Collection<Iterator<T>>
// Type safety: The expression of type Iterator[] needs unchecked conversion 
// to conform to Iterator<T>[]
iterators = initIterators.<Iterator<T>>toArray(
        new Iterator[initIterators.size()]);

// Type safety: The expression of type Iterator[] needs unchecked conversion 
// to conform to Iterator<T>[]
iterators = initIterators.toArray(new Iterator[initIterators.size()]);

// Doesn't compile
iterators = initIterators.toArray(new Iterator<T>[initIterators.size()]);
4

1 回答 1

12

没有类型安全的方法可以创建参数化类型的数组,例如Iterator<T>[].

或者,您可以创建一个原始数组:Iterator<?>[]. 或者,如果您可以完全避免使用数组,请使用像List<Iterator<T>>.

不可能的原因是 Java 数组是协变的,而泛型类型的参数化边界是不变的。也就是说:

Integer[] integers = new Integer[1];
Number[] numbers = integers; // OK because Integer extends Number
numbers[0] = new Double(3.14); // runtime exception

编译器允许赋值,因为DoubleextendsNumber和声明的类型numbersNumber[]。但是在运行时,实际的数组对象实例是原始的Integer[1],并且数组知道它们包含的对象的类型。

对于泛型,参数化类型是不同的。一方面,由于编译时类型擦除,它们本质上并不知道它们的运行时类型。

List<Integer> integerList = new ArrayList<Integer>();

List<Number> numberList = integerList; // compiler error, prevents:
numberList.add(new Double(3.14)); // would insert a Double into integerList

Collection<Integer> integerCollection = integerList; // allowed
// OK because List extends Collection and the <type parameter> did not change

Collection<Number> numberCollection = integerList; // compiler error
// an "Integer" is a "Number"
// but "a collection of Integers" is more specific than "a collection of Numbers"
// and cannot be generally treated the same way and guarantee correct behavior

List<?> rawList = integerList; // allowed, but...
rawList.add(new Integer(42));  // compiler error, Integer is not a ... a what?

使用泛型,在 Java 中,您依赖编译器(而不是运行时)来验证泛型类型是否正确和安全。

因此,虽然 anIterator<?>[]在运行时知道它是一个包含Iterator元素的数组,但<T>in在编译时Iterator<T>[]擦除,运行时无法知道它应该是什么。因此,您会收到未经检查的警告。

于 2013-05-19T03:58:26.410 回答