Example usage for java.lang Class isArray

List of usage examples for java.lang Class isArray

Introduction

In this page you can find the example usage for java.lang Class isArray.

Prototype

@HotSpotIntrinsicCandidate
public native boolean isArray();

Source Link

Document

Determines if this Class object represents an array class.

Usage

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));
    }//w  w w . j a va2s . 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));
    }//from  www  .  j a  v  a  2s  . c o m

}

From source file:Main.java

public static void main(String[] args) {

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

    Class cls = str.getClass();
    boolean arr = cls.isArray();
    System.out.println(arr);//w  w  w  .j a v a  2s  .  c  o m

    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:ArrayFind.java

public static void main(String... args) {
    boolean found = false;
    try {/*w  ww.j ava2  s  .c  o  m*/
        Class<?> cls = Class.forName(args[0]);
        Field[] flds = cls.getDeclaredFields();
        for (Field f : flds) {
            Class<?> c = f.getType();
            if (c.isArray()) {
                found = true;
                out.format(
                        "%s%n" + "           Field: %s%n" + "            Type: %s%n" + "  Component Type: %s%n",
                        f, f.getName(), c, c.getComponentType());
            }
        }
        if (!found) {
            out.format("No array fields%n");
        }

        // production code should handle this exception more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

public static int getDimension(Object array) {
    int dim = 0;// ww  w.j a va2  s  .c  om
    Class c = array.getClass();
    while (c.isArray()) {
        c = c.getComponentType();
        dim++;
    }
    return (dim);
}

From source file:Main.java

public static int getArrayDimension(Object array) {
    int dimension = 0;
    Class c = array.getClass();
    if (!c.isArray()) {
        throw new IllegalArgumentException("Object is not  an  array");
    }/*from   w  w w. j  av a2 s  . c om*/
    while (c.isArray()) {
        dimension++;
        c = c.getComponentType();
    }
    return dimension;
}

From source file:Main.java

public static int getDim(Object array) {
    int dim = 0;//  w  ww.  j av  a 2 s  . c om
    Class cls = array.getClass();
    while (cls.isArray()) {
        dim++;
        cls = cls.getComponentType();
    }
    return dim;
}

From source file:MainClass.java

static Object doubleArray(Object original) {
    Object returnValue = null;/*  w ww .  ja va 2  s. c  o  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:MainClass.java

private static void printType(Object object) {
    Class type = object.getClass();
    if (type.isArray()) {
        Class elementType = type.getComponentType();
        System.out.println("Array of: " + elementType);
        System.out.println("Array size: " + Array.getLength(object));
    }/* w ww . j  a v a  2  s. com*/
}

From source file:Main.java

public static boolean isCollectionMapOrArray(Class<?> type) {
    if (type.isArray())
        return true;
    if (Collection.class.isAssignableFrom(type))
        return true;
    if (Map.class.isAssignableFrom(type))
        return true;
    return false;
}