List of usage examples for java.lang Class isArray
@HotSpotIntrinsicCandidate public native boolean isArray();
From source file:Main.java
public static Object expand(Object a) { Class cl = a.getClass(); if (!cl.isArray()) { return null; }//from w w w .j a v a2 s . c o m 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 arrayExpandAddSingle(Object array, Object elementsToAdd) { Class cl = array.getClass(); 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 boolean isEmptyArray(Object arrayObj) { if (null == arrayObj) { return true; }//from ww w . jav a 2 s . c om Class clazz = arrayObj.getClass(); if (!clazz.isArray()) { return true; } if (Array.getLength(arrayObj) == 0) { return true; } return false; }
From source file:ArrayGrowTest.java
/** * A convenience method to print all elements in an array * /*from ww w . ja v a 2 s. co m*/ * @param a * the array to print. It can be an object array or a primitive type * array */ static void arrayPrint(Object a) { Class cl = a.getClass(); if (!cl.isArray()) return; Class componentType = cl.getComponentType(); int length = Array.getLength(a); System.out.print(componentType.getName() + "[" + length + "] = { "); for (int i = 0; i < Array.getLength(a); i++) System.out.print(Array.get(a, i) + " "); System.out.println("}"); }
From source file:ArrayGrowTest.java
/** * This method grows an array by allocating a new array of the same type and * copying all elements./*from www. j av a 2 s. c o m*/ * * @param a * the array to grow. This can be an object array or a primitive type * array * @return a larger array that contains all elements of a. */ static Object goodArrayGrow(Object a) { Class cl = a.getClass(); if (!cl.isArray()) return null; Class componentType = cl.getComponentType(); int length = Array.getLength(a); int newLength = length * 11 / 10 + 10; Object newArray = Array.newInstance(componentType, newLength); System.arraycopy(a, 0, newArray, 0, length); return newArray; }
From source file:Main.java
public static Object arrayExpandAddElements(Object array, Object[] elementsToAdd) { Class cl = array.getClass(); 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]); }/*w w w .ja v a 2s.c o m*/ return newArray; }
From source file:Main.java
public static Object arrayExpandAddElements(Object array, Collection elementsToAdd) { Class cl = array.getClass(); if (!cl.isArray()) return null; int length = Array.getLength(array); int newLength = length + elementsToAdd.size(); Class componentType = array.getClass().getComponentType(); Object newArray = Array.newInstance(componentType, newLength); System.arraycopy(array, 0, newArray, 0, length); int count = 0; for (Object element : elementsToAdd) { Array.set(newArray, length + count, element); count++;/*from ww w . j a v a2 s . co m*/ } return newArray; }
From source file:ShowClass.java
public static String typeName(Class t) { String brackets = ""; while (t.isArray()) { brackets += "[]"; t = t.getComponentType();//w w w.j a v a 2 s . c om } String name = t.getName(); int pos = name.lastIndexOf('.'); if (pos != -1) name = name.substring(pos + 1); return name + brackets; }
From source file:ArrayExpander.java
public static Object expand(Object array, int newSize) { if (array == null) { return null; }//from www . j a va 2 s .com 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:ArrayExpander.java
public static Object expandAtHead(Object array, int newSize) { if (array == null) { return null; }/*from w w w.ja v a2s . 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, newSize - len, len); return newArray; } } else { throw new ClassCastException("need array"); } }