Example usage for java.lang.reflect Array set

List of usage examples for java.lang.reflect Array set

Introduction

In this page you can find the example usage for java.lang.reflect Array set.

Prototype

public static native void set(Object array, int index, Object value)
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

Source Link

Document

Sets the value of the indexed component of the specified array object to the specified new value.

Usage

From source file:Main.java

public static void main(String[] args) {
    int[] sourceInts = { 12, 78 };
    int[] destInts = new int[2];

    for (int i = 0; i < Array.getLength(sourceInts); i++) {
        Array.set(destInts, i, Array.get(sourceInts, i));
        System.out.println(Array.get(destInts, i));
    }/*from  www  . ja va  2s .c  om*/
    System.out.println(Arrays.toString(destInts));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int[] array = { 1, 2, 3 };
    // Get the value of the third element.
    Object o = Array.get(array, 2);

    // Set the value of the third element.
    Array.set(array, 2, 1);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int[] array = { 1, 2, 3 };
    // Get the value of the third element.
    Object o = Array.get(array, 2);
    System.out.println("o:" + o);
    // Set the value of the third element.
    Array.set(array, 2, 1);
    System.out.println(Arrays.toString(array));
}

From source file:ArrayCreator.java

public static void main(String... args) {
    Matcher m = p.matcher(s);//from w  ww . j a va 2 s.  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

@SuppressWarnings("unchecked")
public static <T> T[] appendToArrayBegining(T[] array, T element, T... elements) {

    Class<?> componentType = array.getClass().getComponentType();

    Object newArray = Array.newInstance(componentType, array.length + 1 + elements.length);

    Array.set(newArray, 0, element);
    if (elements.length > 0) {
        System.arraycopy(elements, 0, newArray, 1, elements.length);
        System.arraycopy(array, 0, newArray, elements.length, array.length);
    } else {//from www. ja  v a 2s.com
        System.arraycopy(array, 0, newArray, 1, array.length);

    }

    return (T[]) newArray;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] collectionToArray(Collection<T> sourceCollection, Class<?> elementClass) {
    Object array = Array.newInstance(elementClass, sourceCollection.size());
    Iterator iterator = sourceCollection.iterator();
    for (int i = 0; i < sourceCollection.size(); i++) {
        Array.set(array, i, iterator.next());
    }/*from   ww w .  j av  a  2 s  .  c  o m*/
    return (T[]) array;
}

From source file:Main.java

@SuppressWarnings("unchecked")
@SafeVarargs/*w ww  . jav a2s. c  om*/
public static <T> T[] appendToArrayEnd(T[] array, T element, T... elements) {

    Class<?> componentType = array.getClass().getComponentType();

    Object newArray = Array.newInstance(componentType, array.length + 1 + elements.length);

    System.arraycopy(array, 0, newArray, 0, array.length);
    Array.set(newArray, array.length, element);
    System.arraycopy(elements, 0, newArray, array.length + 1, elements.length);

    return (T[]) newArray;
}

From source file:Main.java

/**
 * Get an integer array from a list given
 * @param list List with Objects that can be translated to int
 * @return int[]/*from w  ww.  j  a v  a  2  s  . co m*/
 */
public static int[] getIntegerArray(List list) {
    int[] array = new int[list.size()];
    for (int i = 0; i < array.length; i++) {
        Array.set(array, i, getInteger(list.get(i)));
    }
    return array;
}

From source file:Main.java

/**
 * Get a float array from a list given//from w  w w . jav a2 s.co m
 * @param list List with Objects that can be translated to float
 * @return float[]
 */
public static float[] getFloatArray(List list) {
    float[] array = new float[list.size()];
    for (int i = 0; i < array.length; i++) {
        Array.set(array, i, getFloat(list.get(i)));
    }
    return array;
}

From source file:Main.java

/**
 * Get a double array from a list given/*w ww  . j a v  a  2s . c  o  m*/
 * @param list List with Objects that can be translated to double
 * @return double[]
 */
public static double[] getDoubleArray(List list) {
    double[] array = new double[list.size()];
    for (int i = 0; i < array.length; i++) {
        Array.set(array, i, getDouble(list.get(i)));
    }
    return array;
}