Resize an array, System.arraycopy()
import java.lang.reflect.Array;
public class Main {
public static void main(String arg[]) {
String[] s = (String[]) expand(new String[20]);
System.out.println(s.length);
}
public static Object expand(Object a) {
Class cl = a.getClass();
if (!cl.isArray())
return a;
int length = Array.getLength(a);
int newLength = 1000;
Class componentType = a.getClass().getComponentType();
Object newArray = Array.newInstance(componentType, newLength);
System.arraycopy(a, 0, newArray, 0, length);
return newArray;
}
}
Related examples in the same category