package test2;
import org.w3c.dom.ls.LSOutput;
import test.Employee;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
public class Manager extends Employee {
public Manager(String name) {
super(name);
}
public static void main(String[] args) {
int[] a={1,2,3};
System.out.println(a.length);//3
a=(int[])CopyOf(a,10);
System.out.println(a.length);//10
}
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 njiewArray;
}
}
使用反射编写泛型数组
最新推荐文章于 2021-10-06 11:37:32 发布