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

/**
 * Merges two arrays into a new array. Elements from array1 and array2 will
 * be copied into a new array, that has array1.length + array2.length
 * elements.//w w w  .  ja  va2 s .  com
 *
 * @param pArray1 First array
 * @param pArray2 Second array, must be compatible with (assignable from)
 *                the first array
 * @return A new array, containing the values of array1 and array2. The
 *         array (wrapped as an object), will have the length of array1 +
 *         array2, and can be safely cast to the type of the array1
 *         parameter.
 * @see #mergeArrays(Object,int,int,Object,int,int)
 * @see java.lang.System#arraycopy(Object,int,Object,int,int)
 */
public static Object mergeArrays(Object pArray1, Object pArray2) {
    return mergeArrays(pArray1, 0, Array.getLength(pArray1), pArray2, 0, Array.getLength(pArray2));
}

From source file:MainClass.java

private static void displayArray(Object array) {
    int length = Array.getLength(array);
    for (int i = 0; i < length; i++) {
        int value = Array.getInt(array, i);
        System.out.println("Position: " + i + ", value: " + value);
    }/*w  w  w.ja v a 2  s  .com*/
}

From source file:Main.java

public static Object copyOf(Object src) {
    int srcLength = Array.getLength(src);
    Class<?> srcComponentType = src.getClass().getComponentType();
    Object dest = Array.newInstance(srcComponentType, srcLength);
    if (srcComponentType.isArray()) {
        for (int i = 0; i < Array.getLength(src); i++) {
            Array.set(dest, i, copyOf(Array.get(src, i)));
        }//  w  ww  .  j  ava2  s.com
    } else {
        System.arraycopy(src, 0, dest, 0, srcLength);
    }
    return dest;
}

From source file:Main.java

public static Object findElementInstance(Object collection) {
    if (collection == null)
        return null;
    if (collection.getClass().isArray()) {
        for (int i = 0; i < Array.getLength(collection); i++) {
            Object o = Array.get(collection, i);
            if (o != null) {
                return o;
            }/*  w  w w .j  a  v  a2s. c om*/
        }
    } else if (collection instanceof Collection) {
        for (Object o : ((Collection<?>) collection)) {
            if (o != null) {
                return o;
            }
        }
    }
    return null;
}

From source file:Main.java

public static Object arrayExpandAddSingle(Object array, Object elementsToAdd) {
    Class cl = array.getClass();/*from ww  w  .  j  a  va 2  s. c  o  m*/
    if (!cl.isArray())
        return null;
    int length = Array.getLength(array);
    int newLength = length + 1;
    Class componentType = array.getClass().getComponentType();
    Object newArray = Array.newInstance(componentType, newLength);
    System.arraycopy(array, 0, newArray, 0, length);
    Array.set(newArray, length, elementsToAdd);
    return newArray;
}

From source file:Main.java

public static Object arrayExpandAddElements(Object array, Object[] elementsToAdd) {
    Class cl = array.getClass();/*from   w ww  . ja v  a2  s  .co  m*/
    if (!cl.isArray())
        return null;
    int length = Array.getLength(array);
    int newLength = length + elementsToAdd.length;
    Class componentType = array.getClass().getComponentType();
    Object newArray = Array.newInstance(componentType, newLength);
    System.arraycopy(array, 0, newArray, 0, length);
    for (int i = 0; i < elementsToAdd.length; i++) {
        Array.set(newArray, length + i, elementsToAdd[i]);
    }
    return newArray;
}

From source file:Main.java

/**
 * {@link Arrays#toString(Object[])} with {@link Arrays#toString(Object[])} for array elements
 *//*www  . ja  va2  s . co  m*/
public static Object arrayToString(Object obj) {
    if (obj != null && obj.getClass().isArray()) {
        int len = Array.getLength(obj);
        String[] strings = new String[len];
        for (int i = 0; i < len; i++) {
            strings[i] = String.valueOf(Array.get(obj, i));
        }
        return Arrays.toString(strings);
    } else {
        return obj;
    }
}

From source file:Main.java

static public Object expandArray(Object[] list, int newSize) {
    Class type = list.getClass().getComponentType();
    Object temp = Array.newInstance(type, newSize);
    System.arraycopy(list, 0, temp, 0, Math.min(Array.getLength(list), newSize));
    return temp;//from  ww  w . j a  va2s  . co  m
}

From source file:ArrayExpander.java

public static Object expand(Object array, int newSize) {
    if (array == null) {
        return null;
    }//  ww  w. j a  v  a 2 s .c  o  m
    Class c = array.getClass();
    if (c.isArray()) {
        int len = Array.getLength(array);
        if (len >= newSize) {
            return array;
        } else {
            Class cc = c.getComponentType();
            Object newArray = Array.newInstance(cc, newSize);
            System.arraycopy(array, 0, newArray, 0, len);
            return newArray;
        }
    } else {
        throw new ClassCastException("need  array");
    }
}

From source file:Main.java

public static int size(Object object) {
    if (object == null) {
        return 0;
    }//  w w w  . j a  v a2s.  c  o m

    int total = 0;
    if (object instanceof Map) {
        total = ((Map) object).size();
    } else if (object instanceof Collection) {
        total = ((Collection) object).size();
    } else if (object instanceof Object[]) {
        total = ((Object[]) object).length;
    } else if (object instanceof Iterator) {
        Iterator it = (Iterator) object;
        while (it.hasNext()) {
            total++;
            it.next();
        }
    } else if (object instanceof Enumeration) {
        Enumeration it = (Enumeration) object;
        while (it.hasMoreElements()) {
            total++;
            it.nextElement();
        }
    } else {
        try {
            total = Array.getLength(object);
        } catch (IllegalArgumentException ex) {
            throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
        }
    }

    return total;
}