Example usage for java.lang.reflect Array getLength

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

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static native int getLength(Object array) throws IllegalArgumentException;

Source Link

Document

Returns the length of the specified array object, as an int .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object o = new int[1][2][3];
    int len = Array.getLength(o); // 1
    System.out.println(len);//from ww  w . jav  a2 s .  co  m
    int dim = getDim(o); // 3
    System.out.println(dim);
}

From source file:MainClass.java

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

    int length = Array.getLength(array);
    for (int i = 0; i < length; i++) {
        int value = i;
        Array.setInt(array, i, value);
    }/*  w  w  w  .j  a  v  a2 s  .  c o m*/

    for (int i : (int[]) array) {
        System.out.println(i);

    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int[] array = { 1, 2, 3 };
    Object newArray = Array.newInstance(array.getClass().getComponentType(), Array.getLength(array) * 2);
    System.arraycopy(array, 0, newArray, 0, Array.getLength(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  ww w  .  j  a va2 s  .c  o m
    System.out.println(Arrays.toString(destInts));
}

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);
    }/*  w ww .j ava 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:MainClass.java

public static void main(String args[]) {
    int[] object = { 1, 2, 3 };
    Class type = object.getClass();
    if (type.isArray()) {
        Class elementType = type.getComponentType();
        System.out.println("Array of: " + elementType);
        System.out.println("Length: " + Array.getLength(object));
    }/*  ww w  .  j av  a 2s.co m*/
}

From source file:MainClass.java

public static void main(String args[]) {
    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));
    }//ww  w  .j  av a  2 s.c om

}

From source file:Main.java

public static void main(String[] args) {

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

    Class cls = str.getClass();//from   w w  w. j  a  v  a2  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: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);
        }//  w  w w  .  j a  va 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);
                }/* w  ww .j av  a2 s .  c  om*/
            }
        }
    }
}