一般方法:利用java.util.Arrays类中的copyOf方法
public static void main(String[] args) {
String[] a = {"1","2","3","4"};
a = Arrays.copyOf(a, 10);
for(int i = 0;i<a.length; i++){
System.out.println(a[i]);
}
}
利用反射:
public static void main(String[] args) {
String[] a = {"1","2","3","4"};
a = (String[]) arrayGrow(a);
for(int i = 0;i<a.length; i++){
System.out.println(a[i]);
}
}
public static Object arrayGrow (Object o){
Class c = o.getClass();
Class ct = c.getComponentType();
Object no = Array.newInstance(ct,Array.getLength(o)+5);
System.arraycopy(o, 0, no, 0, Array.getLength(o));
return no;
}
使用的类为java.lang.reflect.Array