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

static public Object expandArray(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 w  w  w.  j av a  2  s  .co m*/
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    T[] copy = ((Object) newType == (Object) Object[].class) ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;//from  ww  w .j  a  va2 s .c o m
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] intersect(Class<T> arrayType, T[] first, T[] second) {
    List<T> result = intersect(first, second);
    return result.toArray((T[]) Array.newInstance(arrayType, result.size()));
}

From source file:Main.java

public static <T> T[] combineArrays(Class<T> clazz, T[]... arrays) {
    int length = 0;

    for (T[] array : arrays) {
        if (array == null)
            continue;
        length += array.length;//from  w w w.j a v  a 2s  . com
    }

    T[] finalArray = (T[]) Array.newInstance(clazz, length);

    int i = 0;
    for (T[] array : arrays) {
        if (array == null)
            continue;
        System.arraycopy(array, 0, finalArray, i, array.length);
        i += array.length;
    }

    return finalArray;
}

From source file:Util.java

/*********************************************************************
* Removes duplicate elements from the array.
*********************************************************************/
public static Object[] removeDuplicates(Object[] array)
//////////////////////////////////////////////////////////////////////
{

    Hashtable hashtable = new Hashtable();

    for (int i = 0; i < array.length; i++) {
        hashtable.put(array[i], array[i]);
    }/*  w w w. j  a va 2  s .c o m*/

    Object[] newArray = (Object[]) Array.newInstance(array.getClass().getComponentType(), hashtable.size());

    int index = 0;

    Enumeration enumeration = hashtable.elements();

    while (enumeration.hasMoreElements()) {
        newArray[index++] = enumeration.nextElement();
    }

    return newArray;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] delete(T[] array, int index) {
    int length = array.length;
    if (index < 0 || index >= length) {
        throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
    }/*from  w  w w.  j a  v a  2s.co m*/

    T[] result = (T[]) Array.newInstance(array.getClass().getComponentType(), length - 1);
    System.arraycopy(array, 0, result, 0, index);
    if (index < length - 1) {
        System.arraycopy(array, index + 1, result, index, length - index - 1);
    }

    return result;
}

From source file:Main.java

public final static Class<?> getRawClass(Type type) {
    if (type instanceof Class<?>)
        return (Class<?>) type;
    else if (type instanceof ParameterizedType) {
        ParameterizedType pType = (ParameterizedType) type;
        return (Class<?>) pType.getRawType();
    } else if (type instanceof GenericArrayType) {
        Type componentType = ((GenericArrayType) type).getGenericComponentType();
        return Array.newInstance(getRawClass(componentType), 0).getClass();
    } else/*from   w w  w .j  a  v a2s.  c o m*/
        return getRawClass(expand(type, null));
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] join(T[] head, T[] tail) {
    if (head == null) {
        return tail;
    }//ww  w .j a  v a  2s. c o m
    if (tail == null) {
        return head;
    }
    Class<?> type = head.getClass().getComponentType();
    T[] result = (T[]) Array.newInstance(type, head.length + tail.length);

    System.arraycopy(head, 0, result, 0, head.length);
    System.arraycopy(tail, 0, result, head.length, tail.length);

    return result;
}

From source file:Main.java

/**
 * Joins arrays using provided component type.
 *//*from   w  w w . ja  va2s .  c  o  m*/
@SuppressWarnings({ "unchecked" })
public static <T> T[] join(Class<T> componentType, T[][] arrays) {
    if (arrays.length == 1) {
        return arrays[0];
    }
    int length = 0;
    for (T[] array : arrays) {
        length += array.length;
    }
    T[] result = (T[]) Array.newInstance(componentType, length);

    length = 0;
    for (T[] array : arrays) {
        System.arraycopy(array, 0, result, length, array.length);
        length += array.length;
    }
    return result;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] remove(T[] values, T value) {
    if (values != null && value != null) {
        for (int i = 0; i < values.length; i++) {
            T v = values[i];/*from  w  w w  .  ja va2s . co  m*/
            if (value.equals(v)) {
                T[] newValues = (T[]) Array.newInstance(values.getClass().getComponentType(),
                        values.length - 1);
                if (i > 0) {
                    System.arraycopy(values, 0, newValues, 0, i);
                }
                if (i < values.length - 1) {
                    System.arraycopy(values, i + 1, newValues, i, values.length - 1 - i);
                }
                values = newValues;
                i--;
            }
        }
    }
    return values;
}