利用反射编写泛型数组代码
作者:互联网
public static Object copyOf(Object a,int newLength){
Class cl = a.getClass();
if(!cl.isArray()){
return null;
}
Class componentType = cl.getComponentType();
int length = Array.getLength(a);
Object newArray = Array.newInstance(componentType,newLength);
System.arraycopy(a,0,newArray,0,Math.min(length,newLength));
return newArray;
}
标签:Object,newLength,int,cl,newArray,数组,泛型,编写,Class 来源: https://www.cnblogs.com/ludakuan/p/14624556.html