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 extends Object> T[] toArray(List<T> list) {
    if (list == null || list.isEmpty())
        return null;

    T[] array = (T[]) Array.newInstance(list.get(0).getClass(), list.size());
    for (int i = 0; i < array.length; i++)
        array[i] = list.get(i);/*from w ww  . java2s  . c  o  m*/
    return array;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] appendToArrayBegining(T[] array, T element, T... elements) {

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

    Object newArray = Array.newInstance(componentType, array.length + 1 + elements.length);

    Array.set(newArray, 0, element);
    if (elements.length > 0) {
        System.arraycopy(elements, 0, newArray, 1, elements.length);
        System.arraycopy(array, 0, newArray, elements.length, array.length);
    } else {//from  w ww.j  ava  2s  . c  om
        System.arraycopy(array, 0, newArray, 1, array.length);

    }

    return (T[]) newArray;
}

From source file:Main.java

@SuppressWarnings("unchecked")
@SafeVarargs/*from w  w  w . jav a 2  s.c  o  m*/
public static <T> T[] appendToArrayEnd(T[] array, T element, T... elements) {

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

    Object newArray = Array.newInstance(componentType, array.length + 1 + elements.length);

    System.arraycopy(array, 0, newArray, 0, array.length);
    Array.set(newArray, array.length, element);
    System.arraycopy(elements, 0, newArray, array.length + 1, elements.length);

    return (T[]) newArray;
}

From source file:Main.java

public static <M extends Object> M[] subArray(M[] array, int start, int end) {
    end = Math.min(array.length, end);
    if (start >= end)
        return null;

    M[] result = (M[]) Array.newInstance(array[0].getClass(), end - start);
    System.arraycopy(array, start, result, 0, result.length);

    return result;
}

From source file:Main.java

@Nullable
public static <T> T[] toArrayOfType(Class<T> type, @Nullable Parcelable[] original) {
    if (original == null)
        return null;
    T[] newArray = (T[]) Array.newInstance(type, original.length);
    System.arraycopy(original, 0, newArray, 0, original.length);
    return newArray;
}

From source file:Main.java

/**
 * Trims an array to a given size.//from w w w  . j av a 2 s  .  c o  m
 *
 * @param f Array to be trimmed.
 * @return Trimmed array 
 */

static public Object trimArray(Object[] list, int newSize) {
    Class type = list.getClass().getComponentType();
    Object temp = Array.newInstance(type, newSize);
    System.arraycopy(list, 0, temp, 0, newSize);
    return temp;
}

From source file:Main.java

public static <T> T[] subArray(T[] x, int start, int len) {
    if (start + len > x.length)
        throw new IllegalArgumentException("length + start > x.length");
    T[] out = null;//from   ww  w.j av  a  2  s  . co m
    try {
        out = (T[]) Array.newInstance(x.getClass().getComponentType(), len);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
    }
    System.arraycopy(x, start, out, 0, len);
    return out;
}

From source file:Main.java

public static <T> T[] concat(T[] first, T[]... rest) {
    int totalLength = first.length;
    for (T[] arr : rest) {
        totalLength += arr.length;//from  w w  w . j a v a 2  s  . c o  m
    }

    T[] result = (T[]) Array.newInstance(first.getClass(), totalLength);
    System.arraycopy(first, 0, result, 0, first.length);
    int pos = first.length;

    for (T[] arr : rest) {
        System.arraycopy(arr, 0, result, pos, arr.length);
        pos += arr.length;
    }

    return result;
}

From source file:Main.java

public static String[] join(String[] left, String[] right) {
    if (left == null) {
        return clone(right);
    } else if (right == null) {
        return clone(left);
    }/*from w w  w.j av a  2s .c  o m*/
    String[] joined = (String[]) Array.newInstance(String.class, left.length + right.length);
    System.arraycopy(left, 0, joined, 0, left.length);
    System.arraycopy(right, 0, joined, left.length, right.length);
    return joined;
}

From source file:Main.java

public static <T> T[] joinArrays(final Class<T> type, final T[]... arrs) {
    int total = 0;
    for (final T[] arr : arrs) {
        if (arr != null)
            total += arr.length;//from  w  w w .ja  va  2s .c om
    }
    final T[] ret = (T[]) Array.newInstance(type, total);
    int x = 0;
    for (final T[] arr : arrs) {
        if (arr != null) {
            System.arraycopy(arr, 0, ret, x, arr.length);
            x += arr.length;
        }
    }
    return ret;
}