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[] arrayAppend(Class<T> cls, T[] arr, T... append) {
    @SuppressWarnings("unchecked")
    T[] ret = (T[]) Array.newInstance(cls, arr.length + append.length);
    for (int i = 0; i < arr.length; i++)
        ret[i] = arr[i];//from   w ww .  j a  va  2 s  .c om
    for (int i = 0; i < append.length; i++)
        ret[i + arr.length] = append[i];
    return ret;
}

From source file:Main.java

static public Object extractArray(Object[] list, int pos, int n) {
    Class type = list.getClass().getComponentType();
    Object temp = Array.newInstance(type, n);
    System.arraycopy(list, pos, temp, 0, n);
    return temp;//from   www .j a  v  a  2 s. c o m
}

From source file:Main.java

public static <T> T[] addElement(T[] array, T element) {
    Class<?> ct = array.getClass().getComponentType();
    T[] newArray = (T[]) Array.newInstance(ct, array.length + 1);
    System.arraycopy(array, 0, newArray, 0, array.length);
    newArray[newArray.length - 1] = element;
    return newArray;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] toArray(List<T> list, Class<T> clazz) {
    return list.toArray((T[]) Array.newInstance(clazz, list.size()));
}

From source file:Main.java

public static <T> T[] toArray(List<T> list, Class<T> clazz) {
    @SuppressWarnings("unchecked")
    T[] result = (T[]) Array.newInstance(clazz, list.size());
    for (int i = 0; i < list.size(); i++) {
        result[i] = list.get(i);/*from  w  w w .j  av a2 s  .co  m*/
    }
    return result;
}

From source file:Main.java

public static <T> T[] convertArrayToList(Collection<T> list) {
    return (T[]) (list.size() > 0
            ? list.toArray((Object[]) Array.newInstance(list.iterator().next().getClass(), list.size()))
            : null);//w w w . jav a  2  s .  co  m
}

From source file:Main.java

public static <T> T[] resize(T[] x, int size) {
    T[] out = null;//from w  w w .  j  a va  2  s .  c  om
    try {
        out = (T[]) Array.newInstance(x.getClass().getComponentType(), size);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
    }
    int n = Math.min(x.length, size);
    System.arraycopy(x, 0, out, 0, n);
    return out;
}

From source file:Main.java

public static <T> T[] mergeArrays(T[]... arr) {
    int size = 0;
    for (T[] ts : arr) {
        size += ts.length;//from  w  ww . j  a v a  2s.co m
    }
    T[] ret = (T[]) Array.newInstance(arr[0].getClass().getComponentType(), size);
    int off = 0;
    for (T[] ts : arr) {
        System.arraycopy(ts, 0, ret, off, ts.length);
        off += ts.length;
    }
    return ret;
}

From source file:Main.java

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

    if (array.length >= length) {
        return array;
    }//from w ww. j a v a 2s.  c  o  m

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

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] collectionToArray(Collection<T> sourceCollection, Class<?> elementClass) {
    Object array = Array.newInstance(elementClass, sourceCollection.size());
    Iterator iterator = sourceCollection.iterator();
    for (int i = 0; i < sourceCollection.size(); i++) {
        Array.set(array, i, iterator.next());
    }/*from   w w w. j a va  2s  .  c  om*/
    return (T[]) array;
}