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

/**
 * Get a double array from a list given/*from   w  w w.j  a v a2 s .  c o  m*/
 * @param list List with Objects that can be translated to double
 * @param klass Class type of array
 * @return Object array of elements
 */
public static Object getObjectArray(List list, Class klass) {
    Object array = Array.newInstance(klass, list.size());
    for (int i = 0; i < list.size(); i++) {
        Array.set(array, i, list.get(i));
    }
    return array;
}

From source file:Main.java

/**
 * Convert a collection to an array//from   w  w  w  . j a  v a2s.c  o  m
 * 
 * @param col
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T[] toArray(Collection<T> col, Class<? super T> c) {
    int size = col.size();
    T[] array = (T[]) Array.newInstance(c, size);
    col.toArray(array);

    return array;
}

From source file:Main.java

/**
 * Resizes an array of Objects to a specified size.
 *
 * @param list Array to be resized.// w  w w .  j  a  v a2s.  c o  m
 * @param newSize Array size after resizing
 * @return Array of type Object with the specified size
 */
static public Object resizeArray(Object[] list, int newSize) {
    Class type = list.getClass().getComponentType();
    Object temp = Array.newInstance(type, newSize);
    System.arraycopy(list, 0, temp, 0, Math.min(Array.getLength(list), newSize));
    return temp;
}

From source file:Main.java

public static Object combineArray(Object firstArray, Object secondArray) {
    int firstLength = Array.getLength(firstArray);
    int secondLength = Array.getLength(secondArray);
    int length = firstLength + secondLength;

    Class<?> componentType = firstArray.getClass().getComponentType();
    Object newArray = Array.newInstance(componentType, length);
    for (int i = 0; i < length; i++) {
        if (i < firstLength) {
            Array.set(newArray, i, Array.get(firstArray, i));
        } else {/*  www  .j  a v a 2s.c  om*/
            Array.set(newArray, i, Array.get(secondArray, i - firstLength));
        }
    }
    return newArray;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] remove(T[] array, int start, int len, Class<T> clazz) {
    T[] r = (T[]) Array.newInstance(clazz, array.length - len);
    System.arraycopy(array, 0, r, 0, start);
    System.arraycopy(array, start + len, r, start, array.length - start - len);
    return r;//from w w w . java  2s .  c om
}

From source file:Main.java

private static <T> T[] filterNull(T[] views) {
    int end = 0;//from  w w  w .j a v a 2 s .  c  o  m
    int length = views.length;
    for (int i = 0; i < length; i++) {
        T view = views[i];
        if (view != null) {
            views[end++] = view;
        }
    }
    if (end == length) {
        return views;
    }
    //noinspection unchecked
    T[] newViews = (T[]) Array.newInstance(views.getClass().getComponentType(), end);
    System.arraycopy(views, 0, newViews, 0, end);
    return newViews;
}

From source file:Main.java

/**
 * Returns the given array if newsize is the same as existing.
 * Returns a new array of given size, containing as many elements of
 * the original array as it can hold./*from   w ww. j av a  2  s  . c  om*/
 */
public static Object resizeArrayIfDifferent(Object source, int newsize) {

    int oldsize = Array.getLength(source);

    if (oldsize == newsize) {
        return source;
    }

    Object newarray = Array.newInstance(source.getClass().getComponentType(), newsize);

    if (oldsize < newsize) {
        newsize = oldsize;
    }

    System.arraycopy(source, 0, newarray, 0, newsize);

    return newarray;
}

From source file:Main.java

/**
 * Converts the given Collection into an array of the given type.
 *
 * @param collection The collection to convert
 * @param arrayType  The type of array to return
 * @return The new array//from w w w  .j  a  v a 2s  .c  o  m
 */
@SuppressWarnings({ "unchecked" })
public static <E> E[] toArray(Collection<E> collection, Class<? extends E> arrayType) {
    return collection.toArray((E[]) Array.newInstance(arrayType, collection.size()));
}

From source file:ArrayExpander.java

public static Object merge(Object array1, Object array2) {
    if (array1 == null) {
        return null;
    }/*from   w w w .j a v  a 2 s  . c o m*/
    if (array2 == null) {
        return array1;
    }
    Class c = array1.getClass();
    if (c.isArray() && array2.getClass().isArray()) {
        Class cc = c.getComponentType();
        Object newArray = Array.newInstance(cc, Array.getLength(array1) + Array.getLength(array2));
        System.arraycopy(array1, 0, newArray, 0, Array.getLength(array1));
        System.arraycopy(array2, 0, newArray, Array.getLength(array1), Array.getLength(array2));
        return newArray;
    } else {
        throw new ClassCastException("need  array");
    }

}

From source file:Main.java

public static <T> T[] asArrayRemoveNulls(Class<T> a_class, T[] values)
/*     */ {/*  ww w  .  j av a2 s .  c  o  m*/
    /* 509 */ArrayList valueList = new ArrayList(values.length);
    /* 510 */for (Object t : values)
    /*     */ {
        /* 512 */if (t == null)
            /*     */continue;
        /* 514 */valueList.add(t);
        /*     */}
    /*     */
    /* 517 */return (T[]) valueList.toArray((Object[]) (Object[]) Array.newInstance(a_class, valueList.size()));
    /*     */}