Example usage for java.lang.reflect Array get

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

Introduction

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

Prototype

public static native Object get(Object array, int index)
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

Source Link

Document

Returns the value of the indexed component in the specified array object.

Usage

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);/*www. jav  a 2  s . com*/
}

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);/*ww  w  .  j a v a2s.  co  m*/
    System.out.println(Arrays.toString(array));
}

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   w  w  w .ja  va 2 s .  c  om*/
    System.out.println(Arrays.toString(destInts));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    OutputStream fos = new BufferedOutputStream(new FileOutputStream("filename.ps"));
    DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
    StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor,
            DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());

    StreamPrintService service = factories[0].getPrintService(fos);
    Class category = OrientationRequested.class;
    Object o = service.getSupportedAttributeValues(category, null, null);
    if (o == null) {
        System.out.println("Attribute is not supported");
    } else if (o.getClass() == category) {
        System.out.println("irrelevant");
    } else if (o.getClass().isArray()) {
        System.out.println("array");
        for (int i = 0; i < Array.getLength(o); i++) {
            Object v = Array.get(o, i);
            System.out.println(v);
        }//from   www  .ja  v a 2 s . c om
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    OutputStream fos = new BufferedOutputStream(new FileOutputStream("filename.ps"));
    DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
    StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor,
            DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());

    StreamPrintService service = factories[0].getPrintService(fos);
    Class[] cats = service.getSupportedAttributeCategories();
    for (int j = 0; j < cats.length; j++) {
        Attribute attr = (Attribute) service.getDefaultAttributeValue(cats[j]);

        if (attr != null) {
            String attrName = attr.getName();
            String attrValue = attr.toString();

            Object o = service.getSupportedAttributeValues(attr.getCategory(), null, null);
            if (o.getClass().isArray()) {
                for (int k = 0; k < Array.getLength(o); k++) {
                    Object o2 = Array.get(o, k);
                    System.out.println(o2);
                }/*www .  jav a2 s  . c om*/
            }
        }
    }
}

From source file:CreateMatrix.java

public static void main(String... args) {
    Object matrix = Array.newInstance(int.class, 2, 2);
    Object row0 = Array.get(matrix, 0);
    Object row1 = Array.get(matrix, 1);

    Array.setInt(row0, 0, 1);// w  w w.  jav a  2 s.  co m
    Array.setInt(row0, 1, 2);
    Array.setInt(row1, 0, 3);
    Array.setInt(row1, 1, 4);

    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 2; j++)
            out.format("matrix[%d][%d] = %d%n", i, j, ((int[][]) matrix)[i][j]);
}

From source file:org.eclipse.smila.search.utils.param.ParameterSet.java

public static void main(String[] arg) {
    final Log log = LogFactory.getLog(ParameterSet.class);
    try {/*from w ww . j a v  a2s  .  c  o m*/

        // DParameterSet einlesen
        org.w3c.dom.Document d = XMLUtils
                .parse(new java.io.File("d:/anyfinder/af-engine-sdk/xml/param-testcase.xml"), true);
        final DParameterSet pset = DParameterSetCodec.decode(d.getDocumentElement());

        org.w3c.dom.Document d2 = XMLUtils.getDocument("top");
        org.w3c.dom.Element e = d2.getDocumentElement();
        org.w3c.dom.Element pelement = DParameterSetCodec.encode(pset, e);

        XMLUtils.stream(pelement, false, "UTF-8", System.err);

        // DParameterDefinition einlesen
        d = XMLUtils.parse(new java.io.File("d:/anyfinder/af-engine-sdk/xml/paramdef-testcase.xml"), true);
        final DParameterDefinition pdef = DParameterDefinitionCodec.decode(d.getDocumentElement());

        d2 = XMLUtils.getDocument("top");
        e = d2.getDocumentElement();
        pelement = DParameterDefinitionCodec.encode(pdef, e);

        XMLUtils.stream(pelement, false, "UTF-8", System.err);

        // ParameterSet erzeugen
        final ParameterSet ps = new ParameterSet(pset, pdef);

        final Enumeration en = ps.getParameterNames();
        for (; en.hasMoreElements();) {
            final String pname = (String) en.nextElement();
            final String ptype = ps.getParameterType(pname);
            Object value = ps.getParameter(pname);
            if (ptype.indexOf("List") >= 0) {
                String newValue = "";

                for (int i = 0; true; i++) {
                    try {
                        newValue += Array.get(value, i);
                        Array.get(value, i + 1);
                        newValue += ", ";
                    } catch (final ArrayIndexOutOfBoundsException iae) {
                        break;
                    }
                }
                value = newValue;
            }
            if (log.isInfoEnabled()) {
                log.info(pname + "(" + ptype + "):\t" + value);
            }
        }
    } catch (final Exception e) {
        if (log.isErrorEnabled()) {
            log.error(e);
        }
    }
}

From source file:Main.java

public static Object[] getArray(Object val) {
    int len = Array.getLength(val);
    Object[] outputArray = new Object[len];
    for (int i = 0; i < len; ++i) {
        outputArray[i] = Array.get(val, i);
    }/*from w ww  .j a  v  a 2 s.  c o  m*/
    return outputArray;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> List<T> getList(Object... items) {
    List<T> list = new ArrayList<T>();
    if (items.length == 1 && items[0].getClass().isArray()) {
        int length = Array.getLength(items[0]);
        for (int i = 0; i < length; i++) {
            Object element = Array.get(items[0], i);
            T item = (T) element;/*from  w  w  w . j a va2  s  .c  o  m*/
            list.add(item);
        }
    } else {
        for (Object i : items) {
            T item = (T) i;
            list.add(item);
        }
    }
    return list;
}

From source file:Main.java

public static Object copyArray(final Object input) {
    final Class type = input.getClass();
    if (!type.isArray()) {
        throw new IllegalArgumentException();
    }//from w  w  w .  ja va2s  .c  o  m
    final int length = Array.getLength(input);
    final Class componentType = type.getComponentType();

    final Object result = Array.newInstance(componentType, length);
    for (int idx = 0; idx < length; idx++) {
        Array.set(result, idx, Array.get(input, idx));
    }
    return result;
}