Example usage for java.lang.reflect Array newInstance

List of usage examples for java.lang.reflect Array newInstance

Introduction

In this page you can find the example usage for java.lang.reflect Array newInstance.

Prototype

public static Object newInstance(Class<?> componentType, int... dimensions)
        throws IllegalArgumentException, NegativeArraySizeException 

Source Link

Document

Creates a new array with the specified component type and dimensions.

Usage

From source file:Main.java

/**
 * Returns an array containing all of the elements of the
 * Iterator//from  w  w  w  . ja va  2  s  .  c  om
 * @param iterator Iterator to copy the contexts of
 * @return an array containing a copy of the iterator contents
 */
public static <T> T[] toArray(Iterator<? extends T> iterator, Class<T> type) {
    if (iterator.hasNext()) {
        Collection arrayList = arrayList(iterator);
        T[] outArray = (T[]) Array.newInstance(type, arrayList.size());

        return (T[]) arrayList.toArray(outArray);
    } else {
        // optimize empty iterator case
        return (T[]) Array.newInstance(type, 0);
    }
}

From source file:Main.java

/**
 * Returns an empty array of the specified type.  The intent is that
 * it will return the same empty array every time to avoid reallocation,
 * although this is not guaranteed.// www.  ja  v a2 s.c  o m
 */
public static <T> T[] emptyArray(Class<T> kind) {
    if (kind == Object.class) {
        return (T[]) EMPTY;
    }

    int bucket = ((System.identityHashCode(kind) / 8) & 0x7FFFFFFF) % CACHE_SIZE;
    Object cache = sCache[bucket];

    if (cache == null || cache.getClass().getComponentType() != kind) {
        cache = Array.newInstance(kind, 0);
        sCache[bucket] = cache;

        // Log.e("cache", "new empty " + kind.getName() + " at " + bucket);
    }

    return (T[]) cache;
}

From source file:Util.java

/*********************************************************************
* Inserts an Object into an Object array at the index position.
*
* <p>//from   www . j a va 2 s  .com
* Example:
* <code>
* <pre>
* String [ ]  stringArray
*   = ( String [ ] ) ArrayLib.insert ( new String [ ] { }, "", 0 );
* </pre>
* </code>
* </p>
*
* @throws NullArgumentException
*
*   If objectArray or o is null.
*
* @throws IndexOutOfBoundsException
*
*   If index < 0 or index > objectArray.length.
*
* @return
*
*   Returns a new array with the same component type as the old array.
*********************************************************************/
public static Object[] insert(Object[] objectArray, Object o, int index)
//////////////////////////////////////////////////////////////////////
{

    if ((index < 0) || (index > objectArray.length)) {
        throw new IndexOutOfBoundsException("index out of range:  " + index);
    }

    Object[] newObjectArray = (Object[]) Array.newInstance(objectArray.getClass().getComponentType(),
            objectArray.length + 1);

    System.arraycopy(objectArray, 0, newObjectArray, 0, index);

    newObjectArray[index] = o;

    System.arraycopy(objectArray, index, newObjectArray, index + 1, objectArray.length - index);

    return newObjectArray;
}

From source file:Main.java

/**
 * Returns an empty array of the specified type.  The intent is that
 * it will return the same empty array every time to avoid reallocation,
 * although this is not guaranteed.//from w  w w  .jav a  2s .  c  o m
 */
@SuppressWarnings("unchecked")
public static <T> T[] emptyArray(Class<T> kind) {
    if (kind == Object.class) {
        //            return (T[]) EmptyArray.OBJECT;
        return (T[]) new Object[0];
    }

    int bucket = (kind.hashCode() & 0x7FFFFFFF) % CACHE_SIZE;
    Object cache = sCache[bucket];

    if (cache == null || cache.getClass().getComponentType() != kind) {
        cache = Array.newInstance(kind, 0);
        sCache[bucket] = cache;

        // Log.e("cache", "new empty " + kind.getName() + " at " + bucket);
    }

    return (T[]) cache;
}

From source file:Main.java

/**
 * Returns an empty array of the specified type.  The intent is that
 * it will return the same empty array every time to avoid reallocation,
 * although this is not guaranteed./*from   w w w .  j a  v  a 2 s  .c  o m*/
 */
@SuppressWarnings("unchecked")
public static <T> T[] emptyArray(Class<T> kind) {
    if (kind == Object.class) {
        return (T[]) EMPTY;
    }

    int bucket = ((System.identityHashCode(kind) / 8) & 0x7FFFFFFF) % CACHE_SIZE;
    Object cache = sCache[bucket];

    if (cache == null || cache.getClass().getComponentType() != kind) {
        cache = Array.newInstance(kind, 0);
        sCache[bucket] = cache;

        // Log.e("cache", "new empty " + kind.getName() + " at " + bucket);
    }

    return (T[]) cache;
}

From source file:Main.java

/**
 * Returns an empty array of the specified type.  The intent is that
 * it will return the same empty array every time to avoid reallocation,
 * although this is not guaranteed./*from w  ww. j ava 2 s. c o  m*/
 */
@SuppressWarnings("unchecked")
public static <T> T[] emptyArray(final Class<T> kind) {
    if (kind == Object.class) {
        return (T[]) EMPTY;
    }

    final int bucket = ((System.identityHashCode(kind) / 8) & 0x7FFFFFFF) % CACHE_SIZE;
    Object cache = sCache[bucket];

    if (cache == null || cache.getClass().getComponentType() != kind) {
        cache = Array.newInstance(kind, 0);
        sCache[bucket] = cache;

        // Log.e("cache", "new empty " + kind.getName() + " at " + bucket);
    }

    return (T[]) cache;
}

From source file:Main.java

/**
 * Change the type of array of Objects to an array of objects of type
 * newClass.// ww w  .j  a v  a  2 s.com
 * 
 */
@SuppressWarnings("unchecked")
public static <T> T[] changeArrayType(Object[] array, Class<T> newClass) {

    ArrayList<T> newArray = new ArrayList<T>();

    for (int i = 0; i < array.length; i++) {
        // Only add those objects that can be cast to the new class
        if (newClass.isInstance(array[i])) {
            newArray.add(newClass.cast(array[i]));
        }
    }

    return newArray.toArray((T[]) Array.newInstance(newClass, 0));
}

From source file:Main.java

/**
 * If the newValue is already held within the values array then the values
 * array is returned, otherwise a new array is created appending the
 * newValue to the end.//  w ww.j  a v  a 2 s  .c  o m
 *
 * @param <T>
 * @param values
 * @param newValue
 * @return an array containing the union of values and newValue
 */
@Deprecated
public static <T> T[] addWithoutDuplicates(T[] values, T newValue) {

    for (T value : values) {
        if (value.equals(newValue)) {
            return values;
        }
    }

    T[] largerOne = (T[]) Array.newInstance(values.getClass().getComponentType(), values.length + 1);
    System.arraycopy(values, 0, largerOne, 0, values.length);
    largerOne[values.length] = newValue;
    return largerOne;
}

From source file:Main.java

/**
 * Method to merge two arrays./*from   w  w w .j a va 2  s .  c  om*/
 * 
 * @param firstObject
 *            The first array to be merged
 * 
 * @param secondObject
 *            The second array to be merged
 * 
 * @return an object containing the elements of the merged arrays
 */
private static Object joinArrays(Object firstObject, Object secondObject) {
    Class<?> o1Type = firstObject.getClass().getComponentType();
    Class<?> o2Type = secondObject.getClass().getComponentType();

    if (o1Type != o2Type)
        throw new IllegalArgumentException();

    int firstObjectSize = Array.getLength(firstObject);
    int secondObjectSize = Array.getLength(secondObject);
    Object array = Array.newInstance(o1Type, firstObjectSize + secondObjectSize);

    int offset = 0, i;
    for (i = 0; i < firstObjectSize; i++, offset++)
        Array.set(array, offset, Array.get(firstObject, i));
    for (i = 0; i < secondObjectSize; i++, offset++)
        Array.set(array, offset, Array.get(secondObject, i));

    return array;
}

From source file:Main.java

/**
 * 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.//from  w  ww  .jav a2 s.c om
 * 
 * @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);
}