List of usage examples for java.lang.reflect Array getLength
@HotSpotIntrinsicCandidate public static native int getLength(Object array) throws IllegalArgumentException;
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 om return outputArray; }
From source file:Main.java
public static int getLength(Object array) { if (array == null) return 0; else/*from w w w . j a va 2 s . co m*/ return Array.getLength(array); }
From source file:Main.java
/** * Returns a duplicates of an array./* w ww . j av a 2 s . co m*/ */ public static Object duplicateArray(Object source) { int size = Array.getLength(source); Object newarray = Array.newInstance(source.getClass().getComponentType(), size); System.arraycopy(source, 0, newarray, 0, size); return newarray; }
From source file:Main.java
public static Object combineArray(Object firstArray, Object secondArray) { int firstLength = Array.getLength(firstArray); int secondLength = Array.getLength(secondArray); int length = firstLength + secondLength; Class<?> componentType = firstArray.getClass().getComponentType(); Object newArray = Array.newInstance(componentType, length); for (int i = 0; i < length; i++) { if (i < firstLength) { Array.set(newArray, i, Array.get(firstArray, i)); } else {/*www . j av a 2s .c o m*/ Array.set(newArray, i, Array.get(secondArray, i - firstLength)); } } return newArray; }
From source file:Main.java
public static Object expand(Object a) { Class cl = a.getClass();/*from ww w.ja va2 s . co m*/ if (!cl.isArray()) { return null; } int length = Array.getLength(a); int newLength = length + 1; // 50% more Class componentType = a.getClass().getComponentType(); Object newArray = Array.newInstance(componentType, newLength); System.arraycopy(a, 0, newArray, 0, length); return newArray; }
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 .j a v a 2 s. co 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; }
From source file:Main.java
public static boolean isEmptyArray(Object arrayObj) { if (null == arrayObj) { return true; }/*from w w w . ja va2 s . c om*/ Class clazz = arrayObj.getClass(); if (!clazz.isArray()) { return true; } if (Array.getLength(arrayObj) == 0) { return true; } return false; }
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 v a 2 s. co m*/ list.add(item); } } else { for (Object i : items) { T item = (T) i; list.add(item); } } return list; }
From source file:ArrayEnumerationFactory.java
static public Enumeration makeEnumeration(final Object obj) { Class type = obj.getClass();/*w w w .ja v a 2 s .c o m*/ if (!type.isArray()) { throw new IllegalArgumentException(obj.getClass().toString()); } else { return (new Enumeration() { int size = Array.getLength(obj); int cursor; public boolean hasMoreElements() { return (cursor < size); } public Object nextElement() { return Array.get(obj, cursor++); } }); } }
From source file:Main.java
/** * Returns the given array if newsize is the same as existing. * Returns a new array of given size, containing as many elements of * the original array as it can hold.//w ww .j a v a 2 s. co m */ public static Object resizeArrayIfDifferent(Object source, int newsize) { int oldsize = Array.getLength(source); if (oldsize == newsize) { return source; } Object newarray = Array.newInstance(source.getClass().getComponentType(), newsize); if (oldsize < newsize) { newsize = oldsize; } System.arraycopy(source, 0, newarray, 0, newsize); return newarray; }