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 void main(String[] args) {

    String str = "This is from java2s.com";

    Class cls = str.getClass();//  w  ww . j av  a 2 s  .  c  om
    boolean arr = cls.isArray();
    System.out.println(arr);

    Object array = Array.newInstance(int.class, 3);

    Class type = array.getClass();
    if (type.isArray()) {
        Class elementType = type.getComponentType();
        System.out.println("Array of: " + elementType);
        System.out.println("Array size: " + Array.getLength(array));
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    Object array = Array.newInstance(int.class, 3);

    int length = Array.getLength(array);
    Random generator = new Random(System.currentTimeMillis());
    for (int i = 0; i < length; i++) {
        int random = generator.nextInt();
        Array.setInt(array, i, random);
    }//from  ww w.jav  a 2 s.  co  m

    for (int i = 0; i < length; i++) {
        int value = Array.getInt(array, i);
        System.out.println("Position: " + i + ", value: " + value);
    }
}

From source file:SampleMultiArrayReflection.java

public static void main(String[] args) {

    // The oneDimA and oneDimB objects are one dimensional int arrays
    // with 5 elements.

    int[] dim1 = { 5 };
    int[] oneDimA = (int[]) Array.newInstance(int.class, dim1);
    int[] oneDimB = (int[]) Array.newInstance(int.class, 5);

    // The twoDimStr object is a 5 X 10 array of String objects.

    int[] dimStr = { 5, 10 };
    String[][] twoDimStr = (String[][]) Array.newInstance(String.class, dimStr);

    // The twoDimA object is an array of 12 int arrays. The tail
    // dimension is not defined. It is equivalent to the array
    // created as follows:
    //    int[][] ints = new int[12][];

    int[] dimA = { 12 };
    int[][] twoDimA = (int[][]) Array.newInstance(int[].class, dimA);
}

From source file:ArrayTrouble.java

public static void main(String... args) {
    Object o = Array.newInstance(int.class, 0);
    int[] i = (int[]) o;
    int[] j = new int[0];
    out.format("i.length = %d, j.length = %d, args.length = %d%n", i.length, j.length, args.length);
    Array.getInt(o, 0); // ArrayIndexOutOfBoundsException
}

From source file:ArrayCreator.java

public static void main(String... args) {
    Matcher m = p.matcher(s);/*  w ww .j a v  a 2s. co  m*/

    if (m.find()) {
        String cName = m.group(1);
        String[] cVals = m.group(2).split("[\\s,]+");
        int n = cVals.length;

        try {
            Class<?> c = Class.forName(cName);
            Object o = Array.newInstance(c, n);
            for (int i = 0; i < n; i++) {
                String v = cVals[i];
                Constructor ctor = c.getConstructor(String.class);
                Object val = ctor.newInstance(v);
                Array.set(o, i, val);
            }

            Object[] oo = (Object[]) o;
            out.format("%s[] = %s%n", cName, Arrays.toString(oo));

            // production code should handle these exceptions more gracefully
        } catch (ClassNotFoundException x) {
            x.printStackTrace();
        } catch (NoSuchMethodException x) {
            x.printStackTrace();
        } catch (IllegalAccessException x) {
            x.printStackTrace();
        } catch (InstantiationException x) {
            x.printStackTrace();
        } catch (InvocationTargetException x) {
            x.printStackTrace();
        }
    }
}

From source file:Main.java

public static <T> T[] insert(T[] array, int index, T element) {
    T[] ret = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length + 1);
    for (int i = 0; i < ret.length; ++i) {
        ret[i] = i < index ? array[i] : (i == index ? element : array[i - 1]);
    }/*w  w w. java  2 s .c  o m*/
    return ret;
}

From source file:Main.java

public static <T> T[] newArray(final Class<T[]> pClass, final int pLength) {
    return pClass.cast(Array.newInstance(pClass.getComponentType(), pLength));
}

From source file:Main.java

public static <U> U[] removeIndex(U[] original, Class<U> originalClass, int element) {
    U[] n = (U[]) Array.newInstance(originalClass, original.length - 1);
    System.arraycopy(original, 0, n, 0, element);
    System.arraycopy(original, element + 1, n, element, original.length - element - 1);
    return n;//from  w  w  w. j a va2  s.c  o  m
}

From source file:Main.java

public static Object toArray(List<?> list, Class<?> componentType) {
    Object result = Array.newInstance(componentType, list.size());
    System.arraycopy(list.toArray(), 0, result, 0, list.size());
    return result;
}

From source file:Main.java

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