Append item to array : Auto Growth Array « Collections Data Structure « Java






Append item to array

      

//Copyright 2007-2009 David Yu dyuproject@gmail.com
//------------------------------------------------------------------------
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at 
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

//package com.dyuproject.util;

import java.lang.reflect.Array;

/**
 * @author David Yu
 * @created Jan 16, 2009
 */

public final class ArrayUtil
{
    
    /**
     * Copies the elements of the {@code oldArray} to a new array with extra space to 
     * append the given element {@code toAppend}.
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] append(T[] oldArray, T toAppend)
    {
        Class<?> component = oldArray.getClass().getComponentType();
        T[] array = (T[])Array.newInstance(component, oldArray.length+1);
        System.arraycopy(oldArray, 0, array, 0, oldArray.length);
        array[oldArray.length] = toAppend;
        return array;
    }    
    
    /**
     * Copies the elements of the {@code oldArray} to a new array with extra space to 
     * append the given element {@code toAppend1} and the array {@code toAppend2}.
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] append(T[] oldArray, T toAppend1, T[] toAppend2)
    {
        Class<?> component = oldArray.getClass().getComponentType();
        T[] array = (T[])Array.newInstance(component, 
                oldArray.length + 1 + toAppend2.length);
        System.arraycopy(oldArray, 0, array, 0, oldArray.length);
        array[oldArray.length] = toAppend1;
        System.arraycopy(toAppend2, 0, array, oldArray.length+1, toAppend2.length);
        return array;
    }
    
    /**
     * Copies the elements of the {@code oldArray} to a new array with extra space to 
     * append the given array {@code toAppend}.
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] append(T[] oldArray, T[] toAppend)
    {
        Class<?> component = oldArray.getClass().getComponentType();
        T[] array = (T[])Array.newInstance(component, 
                oldArray.length + toAppend.length);
        System.arraycopy(oldArray, 0, array, 0, oldArray.length);
        System.arraycopy(toAppend, 0, array, oldArray.length, toAppend.length);
        return array;
    }
    
    /**
     * Copies the elements of the {@code oldArray} to a new array with extra space to 
     * append the given array {@code toAppend1} and the element {@code toAppend2}.
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] append(T[] oldArray, T[] toAppend1, T toAppend2)
    {
        Class<?> component = oldArray.getClass().getComponentType();
        T[] array = (T[])Array.newInstance(component, 
                oldArray.length + toAppend1.length + 1);
        System.arraycopy(oldArray, 0, array, 0, oldArray.length);
        System.arraycopy(toAppend1, 0, array, oldArray.length, toAppend1.length);
        array[array.length-1] = toAppend2;
        return array;
    }
    
    /**
     * Copies the elements of the {@code oldArray} to a new array with extra space to 
     * append the given array {@code toAppend1} and the array {@code toAppend2}.
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] append(T[] oldArray, T[] toAppend1, T[] toAppend2)
    {
        Class<?> component = oldArray.getClass().getComponentType();
        T[] array = (T[])Array.newInstance(component, 
                oldArray.length + toAppend1.length + toAppend2.length);
        System.arraycopy(oldArray, 0, array, 0, oldArray.length);
        System.arraycopy(toAppend1, 0, array, oldArray.length, toAppend1.length);
        System.arraycopy(toAppend2, 0, array, oldArray.length + toAppend1.length, toAppend2.length);
        return array;
    }
    
    /**
     * Returns a copy of the old array {@code oldArray} but with the element at the 
     * give index {@code idx} removed.
     * 
     * @throws {@link IllegalArgumentException} if the array index is out of bounds.
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] remove(T[] oldArray, int idx)
    {
        if(idx<0 || idx>=oldArray.length)
            throw new IllegalArgumentException("array index " + idx + " out of bounds");
        
        Class<?> component = oldArray.getClass().getComponentType();
        T[] array = (T[])Array.newInstance(component, oldArray.length-1);
        if(idx==0)
            System.arraycopy(oldArray, 1, array, 0, array.length);
        else
        {
            System.arraycopy(oldArray, 0, array, 0, idx);
            System.arraycopy(oldArray, idx+1, array, idx, array.length-idx);
        }
        return array;
    }

}

   
    
    
    
    
    
  








Related examples in the same category

1.Growable int[]
2.Your own auto-growth Array
3.Long Vector
4.Int Vector (from java-objects-database)
5.ArrayList of int primitives
6.ArrayList of long primitives
7.ArrayList of short primitives
8.ArrayList of double primitives
9.ArrayList of boolean primitives
10.ArrayList of char primitives
11.ArrayList of byte primitives
12.Growable String array with type specific access methods.
13.Auto Size Array
14.Dynamic Int Array
15.Dynamic Long Array
16.Int Array
17.Int Array List
18.ArrayList of float primitives
19.Fast Array
20.Extensible vector of bytes
21.Int Vector
22.A two dimensional Vector
23.Lazy List creation based on ArrayList
24.Append the given Object to the given array
25.Adds all the elements of the given arrays into a new array.
26.Simple object pool
27.A variable length Double Array: expanding and contracting its internal storage array as elements are added and removed.
28.A growable array of bytes
29.Doubles the size of an array
30.Adds the object to the array.
31.Concatenate arrays
32.Double List