Mega Code Archive

 
Categories / Java / Collections Data Structure
 

Appends an Object to an Object array

import java.lang.reflect.Array;      /*********************************************************************      * Array manipulation for Java 1.1+.      *      * <p>      * Java 1.1 compatible.      * </p>      *      * @see      *   ArrayLib2      *      * @version      *   2003-04-07      * @since      *   2001-04-06      * @author      *   <a href="http://croftsoft.com/">David Wallace Croft</a>*/ public class Util{     /*********************************************************************      * Appends an Object to an Object array.      *      * <p>      * Example:      * <code>      * <pre>      * String [ ]  stringArray      *   = ( String [ ] ) ArrayLib.append ( new String [ ] { }, "" );      * </pre>      * </code>      * </p>      *      * @throws NullArgumentException      *      *   If either argument is null.      *      * @return      *      *   Returns a new array with the same component type as the old array.      *********************************************************************/      public static Object [ ]  append ( Object [ ]  oldArray, Object  o )      //////////////////////////////////////////////////////////////////////      {        Object [ ]  newArray = ( Object [ ] ) Array.newInstance (          oldArray.getClass ( ).getComponentType ( ), oldArray.length + 1 );        System.arraycopy ( oldArray, 0, newArray, 0, oldArray.length );        newArray [ oldArray.length ] = o;        return newArray;      } }