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

@SuppressWarnings("unchecked")
public static <T> T[] emptyIfNull(Class<T> clazz, T[] objects) {
    return objects == null ? (T[]) Array.newInstance(clazz, 0) : objects;
}

From source file:Main.java

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

From source file:Main.java

/**
 * Create a new array by slicing a subset out of an existing one.
 *
 * @param <T>   the type of the array elements
 * @param array the array//from   w w  w.j  a v  a 2 s .c o  m
 * @param off   the starting offset of the slice
 * @param len   the length of the slice.
 * @return a slice of the array starting at the given offset and with the specified length
 */
public static <T extends Object> T[] slice(T[] array, int off, int len) {
    len = Math.min(len, array.length - off);
    T[] res = (T[]) Array.newInstance(array.getClass().getComponentType(), len);
    System.arraycopy(array, off, res, 0, len);
    return res;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] createArray(Class<T> cls, int length) {
    return (T[]) Array.newInstance(cls, length);
}

From source file:Main.java

/** Returns a new array even if collection is empty */
public static <T> T[] toArray(Collection<T> c, Class klass) {
    return toArray(c, (T[]) Array.newInstance(klass, c.size()));
}

From source file:Main.java

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

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] toArray(final Collection<T> c, final Class<? extends T[]> newType) {
    final int length = c.size();
    final T[] array = (T[]) Array.newInstance(newType.getComponentType(), length);
    if (length > 0) {
        int i = 0;
        for (T elem : c) {
            array[i++] = elem;//from   w  w w. java  2  s. com
        }
    }
    return array;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <S, D extends S> D[] downcastToArray(Collection<S> source, Class<D> destClass) {
    D[] dest = (D[]) Array.newInstance(destClass, source.size());
    Iterator<S> it = source.iterator();
    for (int i = 0; i < dest.length; i++) {
        S s = it.next();//ww  w  .  ja  v  a 2  s  .c o  m
        if (destClass.isAssignableFrom(s.getClass()))
            dest[i] = (D) s;
        else
            throw new IllegalArgumentException("Could not downcast element from class "
                    + s.getClass().getSimpleName() + " to " + destClass.getSimpleName());
    }
    return dest;
}

From source file:Main.java

public static Object copyOf(Object src) {
    int srcLength = Array.getLength(src);
    Class<?> srcComponentType = src.getClass().getComponentType();
    Object dest = Array.newInstance(srcComponentType, srcLength);
    if (srcComponentType.isArray()) {
        for (int i = 0; i < Array.getLength(src); i++) {
            Array.set(dest, i, copyOf(Array.get(src, i)));
        }//ww w. j  av  a  2 s.  c  om
    } else {
        System.arraycopy(src, 0, dest, 0, srcLength);
    }
    return dest;
}

From source file:Main.java

/**
 * Copies the specified array, truncating or padding with nulls (if necessary)
 * so the copy has the specified length. For all indices that are valid in
 * both the original array and the copy, the two arrays will contain identical
 * values. For any indices that are valid in the copy but not the original,
 * the copy will contain null. Such indices will exist if and only if the
 * specified length is greater than that of the original array. The
 * resulting array is of exactly the same class as the original array.
 *
 * @param original the array to be copied
 * @param newLength the length of the copy to be returned
 * @return a copy of the original array, truncated or padded with nulls to
 * obtain the specified length/*from w ww  .j a va  2  s .  co m*/
 */
public static <T> T[] copyOf(T[] original, int newLength) {
    @SuppressWarnings({ "unchecked" })
    T[] copy = (T[]) Array.newInstance(original.getClass().getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;
}