public class ArrayLenExtend {
public static Object goodCopy(Object oldArray, int newLength) {
//Array类型
Class c = oldArray.getClass();
System.out.println(c.getName());
//元素类型
Class componentType = c.getComponentType();
System.out.println(componentType.getName());
//创立新数组
Object newArray = Array.newInstance(componentType, newLength);
//拷贝
int oldLength = Array.getLength(oldArray);
System.arraycopy(oldArray, 0, newArray, 0, oldLength);
//返回
return newArray;
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5};
a = (int[]) goodCopy(a,10);
System.out.println(a.length);
}
}
发表回复