如何将一般的对象集合传递给我的方法,如下所述:
class Test {
class X<T> {
}
class XI extends X<Integer> {
}
class XD extends X<Double> {
}
class S {
Collection<X<?>> items;
public void addX(Iterable<X<?>> x) {
// Error: The method addAll(Collection<? extends Test.X<?>>)
// in the type Collection<Test.X<?>>
// is not applicable for the arguments (Iterable<Test.X<?>>)
items.addAll(x);
}
}
void fooo() {
X<?>[] x = new X<?>[] {new XI(), new XD()};
S s = new S();
// The method addX(Iterable<Test.X<?>>)
// in the type Test.S is not applicable for the arguments (Test.X<?>[])
s.addX(x);
}
}
由于
答案 0 :(得分:1)
Collection.addAll
预计Collection
,但您将其传递给Iterable
...
您可以使用Arrays class将数组转换为List(这是一个Collection):
Arrays.asList(myArray);