欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入
支持泛型的方法:
import java.util.ArrayList;
import java.util.Collection;
public classTestGenericMethod {
static<T> voidfromArrayToCollection(T[] a, Collection<T> c){
for(To:a){
c.add(o);
}
System.out.println(c.size());
}
public static voidmain(String[] args){
Object[] oa = newObject[100];
Collection<Object>co = new ArrayList<Object>();
fromArrayToCollection(oa,co);
String[] str = newString[200];
Collection<Object>co1 = new ArrayList<Object>();
fromArrayToCollection(str,co1);
Double[] dou = newDouble[200];
Collection<Object>co2 = new ArrayList<Object>();
fromArrayToCollection(dou,co2);
Integer[] Inte = newInteger[100];
Collection<Object>co3 = new ArrayList<Object>();
fromArrayToCollection(Inte,co3);
}
}
Output:
100
200
200
100
[1][2]
并且为之实践了关怀和付出的善举。对于我性情中的易感和怨薄,