Inserts the specified element at the specified position in the array. - Android java.lang

Android examples for java.lang:Array Element

Description

Inserts the specified element at the specified position in the array.

Demo Code

/*/*w w w .  j av  a2s  . c om*/
 * @(#)$Id: ArrayUtils.java 3619 2008-03-26 07:23:03Z yui $
 * 
 * Copyright 2006-2008 Makoto YUI
 * 
 * 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.
 * 
 * Contributors: Makoto YUI - ported from jakarta commons lang
 */
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Random;

public class Main{
    /**
     * <p>
     * Inserts the specified element at the specified position in the array. Shifts the element
     * currently at that position (if any) and any subsequent elements to the right (adds one to their
     * indices).
     * </p>
     * 
     * <p>
     * This method returns a new array with the same elements of the input array plus the given
     * element on the specified position. The component type of the returned array is always the same
     * as that of the input array.
     * </p>
     * 
     * <p>
     * If the input array is <code>null</code>, a new one element array is returned whose component
     * type is the same as the element.
     * </p>
     * 
     * <pre>
     * ArrayUtils.insert(null, 0, null)      = [null]
     * ArrayUtils.insert(null, 0, &quot;a&quot;)       = [&quot;a&quot;]
     * ArrayUtils.insert([&quot;a&quot;], 1, null)     = [&quot;a&quot;, null]
     * ArrayUtils.insert([&quot;a&quot;], 1, &quot;b&quot;)      = [&quot;a&quot;, &quot;b&quot;]
     * ArrayUtils.insert([&quot;a&quot;, &quot;b&quot;], 3, &quot;c&quot;) = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]
     * </pre>
     * 
     * @param array
     *          the array to add the element to, may be <code>null</code>
     * @param index
     *          the position of the new object
     * @param element
     *          the object to add
     * @return A new array containing the existing elements and the new element
     * @throws IndexOutOfBoundsException
     *           if the index is out of range (index < 0 || index > array.length).
     */
    public static <T> T[] insert(final Object array, final int index,
            final Object element) {
        if (array == null) {
            if (index != 0) {
                throw new IndexOutOfBoundsException("Index: " + index
                        + ", Length: 0");
            }
            Object joinedArray = Array.newInstance(
                    element != null ? element.getClass() : Object.class, 1);
            Array.set(joinedArray, 0, element);
            return (T[]) joinedArray;
        }
        int length = getLength(array);
        if (index > length || index < 0) {
            throw new IndexOutOfBoundsException("Index: " + index
                    + ", Length: " + length);
        }
        Object result = Array.newInstance(array.getClass()
                .getComponentType(), length + 1);
        System.arraycopy(array, 0, result, 0, index);
        Array.set(result, index, element);
        if (index < length) {
            System.arraycopy(array, index, result, index + 1, length
                    - index);
        }
        return (T[]) result;
    }
    public static long[] insert(final long[] array, final long element) {
        long[] newArray = (long[]) copyArrayGrow1(array, Long.TYPE);
        newArray[lastIndex(newArray)] = element;
        return newArray;
    }
    public static long[] insert(final long[] vals, final int idx,
            final long val) {
        long[] newVals = new long[vals.length + 1];
        if (idx > 0) {
            System.arraycopy(vals, 0, newVals, 0, idx);
        }
        newVals[idx] = val;
        if (idx < vals.length) {
            System.arraycopy(vals, idx, newVals, idx + 1, vals.length - idx);
        }
        return newVals;
    }
    public static byte[][] insert(final byte[][] vals, final int idx,
            final byte[] val) {
        byte[][] newVals = new byte[vals.length + 1][];
        if (idx > 0) {
            System.arraycopy(vals, 0, newVals, 0, idx);
        }
        newVals[idx] = val;
        if (idx < vals.length) {
            System.arraycopy(vals, idx, newVals, idx + 1, vals.length - idx);
        }
        return newVals;
    }
    /**
     * <p>
     * Returns the length of the specified array. This method can deal with <code>Object</code> arrays
     * and with primitive arrays.
     * </p>
     * 
     * <p>
     * If the input array is <code>null</code>, <code>0</code> is returned.
     * </p>
     * 
     * <pre>
     * ArrayUtils.getLength(null)            = 0
     * ArrayUtils.getLength([])              = 0
     * ArrayUtils.getLength([null])          = 1
     * ArrayUtils.getLength([true, false])   = 2
     * ArrayUtils.getLength([1, 2, 3])       = 3
     * ArrayUtils.getLength([&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]) = 3
     * </pre>
     * 
     * @param array
     *          the array to retrieve the length from, may be null
     * @return The length of the array, or <code>0</code> if the array is <code>null</code>
     * @throws IllegalArgumentException
     *           if the object arguement is not an array.
     */
    public static int getLength(final Object array) {
        if (array == null) {
            return 0;
        }
        return Array.getLength(array);
    }
    /**
     * Returns a copy of the given array of size 1 greater than the argument. The last value of the
     * array is left to the default value.
     * 
     * @param array
     *          The array to copy, must not be <code>null</code>.
     * @param newArrayComponentType
     *          If <code>array</code> is <code>null</code>, create a size 1 array of this type.
     * @return A new copy of the array of size 1 greater than the input.
     */
    private static Object copyArrayGrow1(final Object array,
            final Class newArrayComponentType) {
        if (array != null) {
            int arrayLength = Array.getLength(array);
            Object newArray = Array.newInstance(array.getClass()
                    .getComponentType(), arrayLength + 1);
            System.arraycopy(array, 0, newArray, 0, arrayLength);
            return newArray;
        }
        return Array.newInstance(newArrayComponentType, 1);
    }
    /**
     * Returns the last index of the given array or -1 if empty or null. This method can deal with
     * <code>Object</code> arrays and with primitive arrays. This value is one less than the size
     * since arrays indices are 0-based.</p>
     * 
     * <pre>
     * ArrayUtils.lastIndex(null)            = -1
     * ArrayUtils.lastIndex([])              = -1
     * ArrayUtils.lastIndex([null])          = 0
     * ArrayUtils.lastIndex([true, false])   = 1
     * ArrayUtils.lastIndex([1, 2, 3])       = 2
     * ArrayUtils.lastIndex([&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]) = 2
     * </pre>
     * 
     * @param array
     *          the array to return the last index for, may be null
     * @return the last index, -1 if empty or null
     * @throws IllegalArgumentException
     *           if the object arguement is not an array.
     */
    public static int lastIndex(final Object array) {
        return ArrayUtils.getLength(array) - 1;
    }
}

Related Tutorials