我正在将List接口实现为将数据存储在<T>
类型数组中的类。这会产生以Collection为参数的方法的问题,因为Collection将对象存储为Object
。如何将Object
转换为T
?简单的铸造不起作用。Casting Objects to <T> -type
class MyCollection<T> implements List<T>{
T[] tab;
MyCollection() {
this.tab = (T[])new Object[10];
}
public boolean addAll(Collection c){
for(Object o : c){
o = (T)o;
for(int i = 0; i < tab.length; i++){
tab[i] = o;
}
}
}
}
我想:
public boolean addAll(Collection<? extends T> c)
但它失败:
public boolean retainAll(Collection<?> c)
因为我不能改变集合类型在这里:/
public boolean retainAll(Collection<?> c){
boolean result = false;
for(T t : c){
}
return result;
}
你是如何实现'retainAll(Coll ...)'? – 2010-11-08 04:24:48
编辑与实施 – mastodon 2010-11-08 04:33:32