Mega Code Archive

 
Categories / Java / Collections Data Structure
 

Removes an Object from 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{     /*********************************************************************     * Removes an Object from an Object array.     *     * <p>     * Example:     * <code>     * <pre>     * String [ ]  stringArray     *   = ( String [ ] ) remove ( new String [ ] { "" }, 0 );     * </pre>     * </code>     * </p>     *     * @throws NullArgumentException     *     *   If oldArray is null.     *     * @throws ArrayIndexOutOfBoundsException     *     *   If index < 0 or index >= oldArray.length.     *     * @return     *     *   Returns a new array with the same component type as the old array.     *********************************************************************/     public static Object [ ]  remove ( Object [ ]  oldArray, int  index )     //////////////////////////////////////////////////////////////////////     {       if ( ( index < 0 )         || ( index >= oldArray.length ) )       {         throw new ArrayIndexOutOfBoundsException ( index );       }       Object [ ]  newArray = ( Object [ ] ) Array.newInstance (         oldArray.getClass ( ).getComponentType ( ), oldArray.length - 1 );       System.arraycopy ( oldArray, 0, newArray, 0, index );       System.arraycopy (         oldArray, index + 1, newArray, index, newArray.length - index );       return newArray;     } }