The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.lang.reflect  [17 examples] > Arrays  [6 examples]

e124. Expanding an Array

The length of an array cannot be changed. The closest thing to expanding an array is to create a larger one of the same type and copy the contents from the old array.
    Object newArray = Array.newInstance(array.getClass().getComponentType(), Array.getLength(array)*2);
    System.arraycopy(array, 0, newArray, 0, Array.getLength(array));

 Related Examples
e120. Determining If an Object Is an Array
e121. Getting the Length and Dimensions of an Array Object
e122. Getting the Component Type of an Array Object
e123. Creating an Array
e125. Getting and Setting the Value of an Element in an Array Object

See also: Constructors    Fields    Methods    Modifiers   


© 2002 Addison-Wesley.