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 int getLength(final Object array) {
    if (array == null) {
        return 0;
    }//from w  ww  .j a  v  a  2  s.  com

    return Array.getLength(array);
}

From source file:Main.java

public static Object expandBy(Object oldArray, int increment) {
    Object newArray = null;/*from w  w  w.  ja v  a  2  s . c om*/
    int oldLength = Array.getLength(oldArray);
    int newLength = oldLength + increment;
    Class<?> c = oldArray.getClass();
    newArray = Array.newInstance(c.getComponentType(), newLength);
    System.arraycopy(oldArray, 0, newArray, 0, oldLength);
    return newArray;
}

From source file:Main.java

/**
 * Returns a new array of given size, containing as many elements of
 * the original array as it can hold. N.B. Always returns a new array
 * even if newsize parameter is the same as the old size.
 *///ww w  . j  a v  a2 s  . c om
public static Object resizeArray(Object source, int newsize) {

    Object newarray = Array.newInstance(source.getClass().getComponentType(), newsize);
    int oldsize = Array.getLength(source);

    if (oldsize < newsize) {
        newsize = oldsize;
    }

    System.arraycopy(source, 0, newarray, 0, newsize);

    return newarray;
}

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));
    }/*from  w  ww. j ava 2  s .  c  o m*/
}

From source file:Main.java

/**
 * Resizes an array of Objects to a specified size.
 *
 * @param list Array to be resized./*w w w. j a  v a2  s. com*/
 * @param newSize Array size after resizing
 * @return Array of type Object with the specified size
 */
static public Object resizeArray(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 source file:ArrayExpander.java

public static Object merge(Object array1, Object array2) {
    if (array1 == null) {
        return null;
    }//w w w. j  a v  a  2  s .com
    if (array2 == null) {
        return array1;
    }
    Class c = array1.getClass();
    if (c.isArray() && array2.getClass().isArray()) {
        Class cc = c.getComponentType();
        Object newArray = Array.newInstance(cc, Array.getLength(array1) + Array.getLength(array2));
        System.arraycopy(array1, 0, newArray, 0, Array.getLength(array1));
        System.arraycopy(array2, 0, newArray, Array.getLength(array1), Array.getLength(array2));
        return newArray;
    } else {
        throw new ClassCastException("need  array");
    }

}

From source file:MainClass.java

static Object doubleArray(Object original) {
    Object returnValue = null;//from w  w  w .j a  va 2s . c  om
    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:Main.java

@SuppressWarnings("rawtypes")
public static void notEmpty(Object obj, String message) {
    if (obj == null) {
        throw new IllegalArgumentException(message + " must be specified");
    }/*from ww w. jav a 2s.  com*/
    if (obj instanceof String && obj.toString().trim().length() == 0) {
        throw new IllegalArgumentException(message + " must be specified");
    }
    if (obj.getClass().isArray() && Array.getLength(obj) == 0) {
        throw new IllegalArgumentException(message + " must be specified");
    }
    if (obj instanceof Collection && ((Collection) obj).isEmpty()) {
        throw new IllegalArgumentException(message + " must be specified");
    }
    if (obj instanceof Map && ((Map) obj).isEmpty()) {
        throw new IllegalArgumentException(message + " must be specified");
    }
}

From source file:Main.java

/**
 * Returns an array containing the elements of parameter source, with one
 * element removed or added. Parameter adjust {-1, +1} indicates the
 * operation. Parameter colindex indicates the position at which an element
 * is removed or added. Parameter addition is an Object to add when
 * adjust is +1.//from www.  ja  va2  s  .c  o m
 */
public static Object toAdjustedArray(Object source, Object addition, int colindex, int adjust) {

    int newsize = Array.getLength(source) + adjust;
    Object newarray = Array.newInstance(source.getClass().getComponentType(), newsize);

    copyAdjustArray(source, newarray, addition, colindex, adjust);

    return newarray;
}

From source file:Main.java

public static boolean isEmpty(Object obj) {
    if (obj == null) {
        return true;
    }/*w ww . jav  a  2  s .  c  o  m*/
    if (obj instanceof String && obj.toString().length() == 0) {
        return true;
    }
    if (obj.getClass().isArray() && Array.getLength(obj) == 0) {
        return true;
    }
    if (obj instanceof Collection && ((Collection) obj).isEmpty()) {
        return true;
    }
    if (obj instanceof Map && ((Map) obj).isEmpty()) {
        return true;
    }
    if (obj instanceof SparseArray && ((SparseArray) obj).size() == 0) {
        return true;
    }
    if (obj instanceof SparseBooleanArray && ((SparseBooleanArray) obj).size() == 0) {
        return true;
    }
    if (obj instanceof SparseIntArray && ((SparseIntArray) obj).size() == 0) {
        return true;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        if (obj instanceof SparseLongArray && ((SparseLongArray) obj).size() == 0) {
            return true;
        }
    }
    return false;
}