Resize an array, System.arraycopy()
import java.lang.reflect.Array; publicclass Main { publicstaticvoid main(String arg[]) { String[] s = (String[]) expand(new String[20]); System.out.println(s.length); } publicstatic 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