由于各种原因,我想将列表转换为数组,但是 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()]);