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[] convertToSingleArray(T[][] a_arrayOfArrays, Class<T[]> a_destType)
/*     */ {//from www  . ja  v  a 2 s .  com
    /* 230 */int iTotalLength = 0;
    /* 231 */for (Object[] srcArray : a_arrayOfArrays)
    /*     */ {
        /* 233 */if (srcArray == null)
            /*     */continue;
        /* 235 */iTotalLength += srcArray.length;
        /*     */}
    /*     */
    /* 242 */Object[] dest = (Object[]) (Object[]) Array.newInstance(a_destType.getComponentType(),
            iTotalLength);
    /*     */
    /* 245 */int iDestPointer = 0;
    /* 246 */for (Object[] srcArray : a_arrayOfArrays)
    /*     */ {
        /* 248 */if (srcArray == null)
            /*     */continue;
        /* 250 */System.arraycopy(srcArray, 0, dest, iDestPointer, srcArray.length);
        /* 251 */iDestPointer += srcArray.length;
        /*     */}
    /*     */
    /* 255 */return (T[]) dest;
    /*     */}

From source file:Main.java

public static Object[] toArray(List aList, Class aType) {
    Object[] sameElementsArray = (Object[]) Array.newInstance(aType, aList.size());
    return aList.toArray(sameElementsArray);
}

From source file:Main.java

/**
 * Makes an array based on klass, but casts it to be of type T[]. This is a very
 * unsafe operation and should be used carefully. Namely, you should ensure that
 * klass is a subtype of T, or that klass is a supertype of T *and* that the array
 * will not escape the generic constant *and* that klass is the same as the erasure
 * of T.//w w w . j av a 2s . c o  m
 * @param <T>
 */
@SuppressWarnings("unchecked")
public static <T> T[] mkTArray(Class<?> klass, int size) {
    return (T[]) (Array.newInstance(klass, size));

}

From source file:Main.java

/**
 * Merged all arrays into a single array.
 *//*from ww w  . jav a  2  s .c  o m*/
@SuppressWarnings("unchecked")
public static <T> T[] merge(final T[]... arrays) {
    // Compute total size
    Class<T> type = null;
    int size = 0;
    for (final T[] array : arrays) {
        size += array.length;
        if (type == null && array.length > 0) {
            type = (Class<T>) array[0].getClass();
        }
    }

    // Copy all arrays
    final T[] merged = (T[]) Array.newInstance(type, size);

    int index = 0;
    for (final T[] array : arrays) {
        System.arraycopy(array, 0, merged, index, array.length);
        index += array.length;
    }

    return merged;
}

From source file:Main.java

/**
 * Calls the {@link Collection#toArray(Object[])} method constructing array based on the given argument type.
 *
 * @param <E>/*from w  ww  .j  a v  a2 s . com*/
 *            the element type
 * @param collection
 *            the collection
 * @param type
 *            the type
 * @return the e[]
 */
@SuppressWarnings("unchecked")
public static <E> E[] toArray(Collection<? extends E> collection, Class<E> type) {
    return collection.toArray((E[]) Array.newInstance(type, collection.size()));
}

From source file:Main.java

public static Object expandBy(Object oldArray, int increment) {
    Object newArray = null;/* w ww.  j a v a2s.  co m*/
    int oldLength = Array.getLength(oldArray);
    int newLength = oldLength + increment;
    Class<?> c = oldArray.getClass();
    newArray = Array.newInstance(c.getComponentType(), newLength);
    System.arraycopy(oldArray, 0, newArray, 0, oldLength);
    return newArray;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] newArray(Class<?> componentType, int newSize) {
    return (T[]) Array.newInstance(componentType, newSize);
}

From source file:Main.java

/**
 * Returns subarray.//from  ww  w. j a  va 2 s  .  c om
 */
@SuppressWarnings({ "unchecked" })
public static <T> T[] subarray(T[] buffer, int offset, int length, Class<T> componentType) {
    T[] temp = (T[]) Array.newInstance(componentType, length);
    System.arraycopy(buffer, offset, temp, 0, length);
    return temp;
}

From source file:Main.java

/**
 * Copies a collection into an array./*from  w  w w .ja  v a  2 s  .  com*/
 * 
 * @param collection
 *        is the collection that should be copied to an array
 * @param type
 *        is the class of the elements in the collection
 * @return the array copy
 */
public static <T> T[] toArray(final Collection<T> collection, final Class<T> type) {

    final int size = collection.size();
    @SuppressWarnings("unchecked")
    final T[] array = (T[]) Array.newInstance(type, size);
    collection.toArray(array);

    return array;
}

From source file:Main.java

/**
 * Returns an array containing the elements of parameter source, with one
 * element removed or added. Parameter adjust {-1, +1} indicates the
 * operation. Parameter colindex indicates the position at which an element
 * is removed or added. Parameter addition is an Object to add when
 * adjust is +1./* w w  w.j  a v  a  2s.  co  m*/
 */
public static Object toAdjustedArray(Object source, Object addition, int colindex, int adjust) {

    int newsize = Array.getLength(source) + adjust;
    Object newarray = Array.newInstance(source.getClass().getComponentType(), newsize);

    copyAdjustArray(source, newarray, addition, colindex, adjust);

    return newarray;
}