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

/**
 * /*from w  ww . j a  v  a 2 s.c  om*/
 * 
 */
@SuppressWarnings("unchecked")
public static <T> T[] toArray(Collection<T> collection, Class<T> elementClass, int index, int count) {
    int n = collection.size();
    int end = Math.min(index + count, n);
    Object result = Array.newInstance(elementClass, end - index);
    Iterator<T> it = collection.iterator();
    int resultIndex = 0;
    for (int i = 0; i < end; i++) {
        if (!it.hasNext())
            break;
        Object value = it.next();
        if (i < index)
            continue;
        Array.set(result, resultIndex, value);
        resultIndex++;
    }
    return (T[]) result;
}

From source file:Main.java

/**
 * Inserts one element into another array.
 *//*from  w  w  w .ja va2 s  .co m*/
@SuppressWarnings({ "unchecked" })
public static <T> T[] insert(T[] dest, T src, int offset, Class componentType) {
    T[] temp = (T[]) Array.newInstance(componentType, dest.length + 1);
    System.arraycopy(dest, 0, temp, 0, offset);
    temp[offset] = src;
    System.arraycopy(dest, offset, temp, offset + 1, dest.length - offset);
    return temp;
}

From source file:Main.java

/** Removes an element from the array and returns the new array. */
@SuppressWarnings("unchecked")
public static <T> T[] removeElementFromArray(T element, T[] array) {
    T[] newArray = (T[]) Array.newInstance(element.getClass(), array.length - 1);
    int newIndex = 0;
    for (T arrayElement : array) {
        if (!arrayElement.equals(element)) {
            if (newIndex >= newArray.length) {
                // element is not member of array
                return array;
            }//from www . j  ava2 s  .co m
            newArray[newIndex] = arrayElement;
            newIndex++;
        }
    }
    return newArray;
}

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.
 *
 * @param array The array to copy, must not be {@code null}.
 * @param newArrayComponentType If {@code array} is {@code null}, create a
 * size 1 array of this type./*from w  ww. j a  v a  2  s  .c o m*/
 * @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) {
        final int arrayLength = Array.getLength(array);
        final Object newArray = Array.newInstance(array.getClass().getComponentType(), arrayLength + 1);
        System.arraycopy(array, 0, newArray, 0, arrayLength);
        return newArray;
    }
    return Array.newInstance(newArrayComponentType, 1);
}

From source file:io.rhiot.utils.Reflections.java

@SuppressWarnings("unchecked") // Array#newInstance return Object
public static <T> Class<T[]> classOfArrayOfClass(Class<T> clazz) {
    return (Class<T[]>) Array.newInstance(clazz, 0).getClass();
}

From source file:it.greenvulcano.util.ArrayUtils.java

/**
 * @param a//from  w  w  w .j  a v a 2s  . co m
 * @param b
 * @param type
 * @return the arrays concatenated
 */
public static final Object[] concat(Object[] a, Object[] b, Class<?> type) {
    if (a == null) {
        a = (Object[]) Array.newInstance(type, 0);
    }

    if (b == null) {
        b = (Object[]) Array.newInstance(type, 0);
    }

    Object[] result = (Object[]) Array.newInstance(type, a.length + b.length);

    System.arraycopy(a, 0, result, 0, a.length);
    System.arraycopy(b, 0, result, a.length, b.length);

    return result;
}

From source file:Main.java

private static Object arrayRemoveInternal(Object array, Object elementToRemove) {
    boolean found = false;
    final int oldLength = Array.getLength(array);
    if (oldLength == 0) {
        return array;
    }//from   ww  w .  j  ava  2 s  .  co  m
    final int newLength = oldLength - 1;
    final Object result = Array.newInstance(array.getClass().getComponentType(), newLength);
    int nextIndex = 0;
    for (int i = 0; i < oldLength; i++) {
        final Object e = Array.get(array, i);
        if (e.equals(elementToRemove)) {
            found = true;
        } else {
            if (nextIndex == newLength) {
                break;
            }
            Array.set(result, nextIndex, e);
            nextIndex++;
        }
    }
    if (!found) {
        return array;
    }
    return result;
}

From source file:Main.java

/**
 * Creates an array of the given Collection's elements, but with the given
 * <code>Class</code> as element type. Useful for arrays of objects that
 * implement multiple interfaces and a "typed view" onto these objects is
 * required./* w  w w  .  ja v  a2  s .c om*/
 * 
 * @param objects a Collection of objects
 * @param clazz the desired service type of the new array
 * @return <code>null</code> when objects is <code>null</code>, or a new
 *         array containing the elements of the source array which is typed to
 *         the given <code>clazz</code> parameter.
 * @throws IllegalArgumentException if the <code>clazz</code> argument is
 *             <code>null</code>.
 * @throws ArrayStoreException if the elements in <code>objects</code> cannot
 *             be cast to <code>clazz</code>.
 */
public static <T> T[] toArrayOfComponentType(Collection objects, Class<T> clazz) {
    if (objects == null) {
        return null;
    }

    if (clazz == null) {
        throw new IllegalArgumentException("Array target class must not be null");
    }

    if (objects.isEmpty()) {
        return (T[]) Array.newInstance(clazz, 0);
    }

    int i = 0, size = objects.size();
    T[] result = (T[]) Array.newInstance(clazz, size);
    Iterator iter = objects.iterator();

    while (i < size && iter.hasNext()) {
        result[i++] = (T) iter.next();
    }

    return result;
}

From source file:Util.java

/*********************************************************************
* Creates a new subarray from a larger array.
*
* <p>//from   w  w w. jav a2  s  .  c o  m
* To avoid unnecessary object creation, this method returns the
* original array argument if the requested subarray length is the same
* and the startIndex is 0.  That is to say, if the method arguments
* are such that the algorithm would have created a shallow clone, the
* original array is returned instead.
* </p>
*
* @throws NullArgumentException
*
*   If objectArray is null.
*
* @throws ArrayIndexOutOfBoundsException
*
*   If startIndex, length, or startIndex + length are out of range.
*
* @return
*
*   Returns an array with the same component type as the old array.
*********************************************************************/
public static Object[] subArray(Object[] objectArray, int startIndex, int length)
//////////////////////////////////////////////////////////////////////
{

    if ((startIndex == 0) && (length == objectArray.length)) {
        return objectArray;
    }

    Object[] newArray = (Object[]) Array.newInstance(objectArray.getClass().getComponentType(), length);

    System.arraycopy(objectArray, startIndex, newArray, 0, length);

    return newArray;
}

From source file:Main.java

/**
 * Concatenates a set of string arrays./*from w  w  w . java  2 s.  co  m*/
 *
 * @param arrays the arrays to concatenate
 * @return the concatentation of the arguments
 */
private static <T> T[] concat(Class type, T[]... arrays) {
    int size = 0;
    for (T[] array : arrays) {
        size += array.length;
    }
    T[] result = (T[]) Array.newInstance(type, size);
    int offset = 0;
    for (T[] array : arrays) {
        System.arraycopy(array, 0, result, offset, array.length);
        offset += array.length;
    }
    return result;
}