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:ArrayExpander.java

public static Object expand(Object array, int newSize) {
    if (array == null) {
        return null;
    }// w w  w.  j a  v  a  2  s. com
    Class c = array.getClass();
    if (c.isArray()) {
        int len = Array.getLength(array);
        if (len >= newSize) {
            return array;
        } else {
            Class cc = c.getComponentType();
            Object newArray = Array.newInstance(cc, newSize);
            System.arraycopy(array, 0, newArray, 0, len);
            return newArray;
        }
    } else {
        throw new ClassCastException("need  array");
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <E> E[] array(Class<E> elementClass, Object existingArray, E... elements) {
    if (existingArray == null) {
        return elements;
    }/*www.  j  av  a2 s  .  c o  m*/
    E[] result;
    if (existingArray.getClass().isArray()) {
        int length = Array.getLength(existingArray);
        result = (E[]) Array.newInstance(elementClass, length + elements.length);
        System.arraycopy(existingArray, 0, result, 0, length);
        System.arraycopy(elements, 0, result, length, elements.length);
    } else {
        result = (E[]) Array.newInstance(elementClass, 1 + elements.length);
        result[0] = (E) existingArray;
        System.arraycopy(elements, 0, result, 1, elements.length);
    }

    return result;
}

From source file:Main.java

public static Object expand(Object obj, int size, boolean flag, Class class1) {
    if (obj == null) {
        return Array.newInstance(class1, 1);
    } else {//  w  w w.j  a  v  a  2s .  c  o  m
        return expand(obj, size, flag);
    }
}

From source file:Main.java

public static Object expand(Object obj, int size, boolean flag, Class<?> class1) {
    if (obj == null) {
        return Array.newInstance(class1, 1);
    } else {//from w  ww.  jav a  2s  .  c  om
        return expand(obj, size, flag);
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static final <T> T[] add(final T[] a, final T[] b) {
    if (a != null && b != null) {
        if (a.length != 0 && b.length != 0) {
            T[] array = (T[]) Array.newInstance(a.getClass().getComponentType(), a.length + b.length);
            System.arraycopy(a, 0, array, 0, a.length);
            System.arraycopy(b, 0, array, a.length, b.length);
            return array;
        } else if (b.length == 0) {
            return a;
        } else {/*w  ww  . jav a2s  .  c  o m*/
            return b;
        }
    } else if (b == null) {
        return a;
    } else {
        return b;
    }
}

From source file:Main.java

private static Object[] copyOf(Object[] obj, int newSize, Class newType) {
    Object[] copy = ((Object) newType == (Object) Object[].class) ? (Object[]) new Object[newSize]
            : (Object[]) Array.newInstance(newType.getComponentType(), newSize);
    return copy;//from  w  w  w.j a  va 2s. c  o  m
}

From source file:ArrayUtils.java

/**
 * Creates new array of the same type as given array and adds specified member
 * to the end of new array./* ww  w  .j  a  va 2  s .c om*/
 *  
 * @param array
 * @param member
 * @return new array with member appened
 */
public static Object[] addToArray(Object[] array, Object member) {
    Object[] newArray = (Object[]) Array.newInstance(array.getClass().getComponentType(), array.length + 1);
    System.arraycopy(array, 0, newArray, 0, array.length);
    newArray[array.length] = member;
    return newArray;
}

From source file:Main.java

/**
 * Returns an array of aList items in the same order than the {@link List}.
 *
 * @param aList//from   w  w w  .  ja v  a2s .  com
 *         {@link List} to transform.
 *
 * @return {@link T[]} array of aList items in the same order than the {@link List}.
 *
 * @since 1
 */
public static <T> T[] toArray(List<T> aList) {
    T[] result = (T[]) Array.newInstance(aList.get(0).getClass(), 2);
    result = aList.toArray(result);
    return result;
}

From source file:Main.java

/**
 * Appends an element to a copy of the array and returns the copy.
 * @param array The original array, or null to represent an empty array.
 * @param element The element to add./*from ww w. j ava  2  s  .co m*/
 * @return A new array that contains all of the elements of the original array
 * with the specified element added at the end.
 */
@SuppressWarnings("unchecked")
public static <T> T[] appendElement(Class<T> kind, T[] array, T element) {
    final T[] result;
    final int end;
    if (array != null) {
        end = array.length;
        result = (T[]) Array.newInstance(kind, end + 1);
        System.arraycopy(array, 0, result, 0, end);
    } else {
        end = 0;
        result = (T[]) Array.newInstance(kind, 1);
    }
    result[end] = element;
    return result;
}

From source file:Main.java

private static Object collectionToArray(Iterator it, Class componentTypeClass, int size) {
    int index = 0;
    Object array = Array.newInstance(componentTypeClass, size);
    while (it.hasNext()) {
        Object item = it.next();//from   w  w  w  .ja v a  2  s  . c om
        Array.set(array, index, item);
        index++;
    }
    return array;
}