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 trimArray(Object[] list, int start, int end) {
    int newSize = end - start;
    Class type = list.getClass().getComponentType();
    Object temp = Array.newInstance(type, newSize);
    System.arraycopy(list, start, temp, 0, newSize);
    return temp;//from  w ww .j a va 2  s .c o m
}

From source file:MainClass.java

static Object doubleArray(Object original) {
    Object returnValue = null;/*w  w  w. j  a v  a  2s  . co  m*/
    Class type = original.getClass();
    if (type.isArray()) {
        int length = Array.getLength(original);
        Class elementType = type.getComponentType();
        returnValue = Array.newInstance(elementType, length * 2);
        System.arraycopy(original, 0, returnValue, 0, length);
    }
    return returnValue;
}

From source file:Main.java

/**
 * Expands an array of Objects to double of its current size. Remember to use cast to turn the result into an array of the appropriate class, i.e:
 * /*from   ww w  . j  av a  2s. c o  m*/
 *   <code>someArray=(SomeClass [])unlekker.util.Util.expandArray(someArray);
 * @param list Array to be expanded.
 * @return Array of Object [], must be cast to appropriate class.
 */
static public Object expandArray(Object[] list) {
    int newSize = list.length * 2;
    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

@SuppressWarnings("unchecked")
public static <T> T[] withoutFirst(T[] o) {
    final Class<?> memberClass = o.getClass().getComponentType();
    final T[] result = (T[]) Array.newInstance(memberClass, o.length - 1);
    System.arraycopy(o, 1, result, 0, result.length);
    return result;
}

From source file:Main.java

/**
 * Removes sub-array./* w  w w .  ja  va  2s.c om*/
 */
@SuppressWarnings({ "unchecked" })
public static <T> T[] remove(T[] buffer, int offset, int length, Class<T> componentType) {
    int len2 = buffer.length - length;
    T[] temp = (T[]) Array.newInstance(componentType, len2);
    System.arraycopy(buffer, 0, temp, 0, offset);
    System.arraycopy(buffer, offset + length, temp, offset, len2 - offset);
    return temp;
}

From source file:Main.java

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

From source file:Main.java

public static String[] addAll(String[] array1, String[] array2) {
    if (array1 == null) {
        return clone(array2);
    } else if (array2 == null) {
        return clone(array1);
    }/*from ww  w  .j av  a  2s  .c om*/
    String[] joinedArray = (String[]) Array.newInstance(array1.getClass().getComponentType(),
            array1.length + array2.length);
    System.arraycopy(array1, 0, joinedArray, 0, array1.length);
    System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
    return joinedArray;
}

From source file:Main.java

public static <T> T[] toTypedArray(Collection<?> collection, Class<T> type) {
    @SuppressWarnings("unchecked")
    T[] array = (T[]) Array.newInstance(type, collection.size());
    Object[] data = collection.toArray();
    for (int i = 0; i < array.length; i++) {
        array[i] = type.cast(data[i]);//from   w w  w . jav  a 2  s  .c o  m
    }
    return array;

}

From source file:Main.java

public static int[] newUnpaddedIntArray(int minLen) {
    return (int[]) Array.newInstance(int.class, minLen);
}

From source file:Main.java

public static char[] newUnpaddedCharArray(int minLen) {
    return (char[]) Array.newInstance(char.class, minLen);
}