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

public static <T> T[] ensureExactLength(T[] array, int length) {

    if (array.length == length) {
        return array;
    }/*  ww w.java  2  s.  c o m*/

    T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(), length);
    if (array.length > 0) {
        System.arraycopy(array, 0, newArray, 0, length);
    }

    return newArray;
}

From source file:Main.java

/**
 * Returns a duplicates of an array./*from ww  w . j a v  a  2  s . com*/
 */
public static Object duplicateArray(Object source) {

    int size = Array.getLength(source);
    Object newarray = Array.newInstance(source.getClass().getComponentType(), size);

    System.arraycopy(source, 0, newarray, 0, size);

    return newarray;
}

From source file:Main.java

public static Object[] invert(Object[] array, Class aType) {
    int length = array.length;
    Object[] invertedArray = (Object[]) Array.newInstance(aType, length);

    for (int i = 0; i < invertedArray.length; i++)
        invertedArray[i] = array[length - i - 1];

    return invertedArray;
}

From source file:Main.java

public static <T> T[] list2Array(List<T> list, Class<T> clazz) {
    if (list != null) {
        @SuppressWarnings("unchecked")
        T[] array = (T[]) Array.newInstance(clazz, list.size());
        for (int i = 0; i < list.size(); i++) {
            array[i] = list.get(i);/*from   w ww .  j  av  a2 s .  c o  m*/
        }
        return array;
    }
    return null;
}

From source file:Main.java

public static Object[][] toMatrix(Iterator[] iterators, Class eachElementType) {
    Object[][] matrix = (Object[][]) Array.newInstance(eachElementType, new int[] { iterators.length, 0 });
    for (int i = 0; i < iterators.length; i++)
        matrix[i] = toArray(iterators[i], eachElementType);

    return matrix;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] listToArray(List<T> list) {
    if (list == null) {
        return null;
    }/*  ww w .  j  a va 2  s . co m*/

    T[] tArr = (T[]) Array.newInstance(list.get(0).getClass(), list.size());
    return list.toArray(tArr);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] addToArray(T[] array1, T[] array2) {

    Class<?> componentType = array1.getClass().getComponentType();

    final Object newArray = Array.newInstance(componentType, array1.length + array2.length);

    System.arraycopy(array1, 0, newArray, 0, array1.length);
    System.arraycopy(array2, 0, newArray, array1.length, array2.length);

    return (T[]) newArray;
}

From source file:Main.java

public static <T> T[] concatenate(T[] a, T[] b) {
    int aLen = a.length;
    int bLen = b.length;

    @SuppressWarnings("unchecked")
    T[] c = (T[]) Array.newInstance(a.getClass().getComponentType(), aLen + bLen);
    System.arraycopy(a, 0, c, 0, aLen);
    System.arraycopy(b, 0, c, aLen, bLen);

    return c;//from   w  w  w . j av  a 2 s .c  o  m
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] toArray(Collection<T> c) {
    if (c == null) {
        return null;
    } else {// ww w .  j av  a2 s .  c  om
        T[] result = (T[]) Array.newInstance(c.getClass().getComponentType(), c.size());
        int i = 0;
        for (T r : c) {
            result[i] = r;
            i++;
        }
        return result;
    }
}

From source file:Main.java

public static Object newArray(Class<?> claxx, int size) {
    return Array.newInstance(claxx, size);
}