List of usage examples for java.lang Class getComponentType
public Class<?> getComponentType()
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 www . j a v a2 s. c o m*/ }
From source file:Main.java
public static int getDim(Object array) { int dim = 0;/*from ww w . j a v a 2 s .c o m*/ Class c = array.getClass(); while (c.isArray()) { c = c.getComponentType(); dim++; } return (dim); }
From source file:Main.java
public static <T> T[] newArray(final Class<T[]> pClass, final int pLength) { return pClass.cast(Array.newInstance(pClass.getComponentType(), pLength)); }
From source file:SampleComponentReflection.java
static void printComponentType(Object array) { Class arrayClass = array.getClass(); String arrayName = arrayClass.getName(); Class componentClass = arrayClass.getComponentType(); String componentName = componentClass.getName(); System.out.println("Array: " + arrayName + ", Component: " + componentName); }
From source file:Main.java
/** * Same as a readable name but internal class keep the dollar sign in their name * @param elt// w w w. j av a 2 s . co m * @return */ public static String readableSootName(Class<?> elt) { if (elt.isArray()) return readableSootName(elt.getComponentType()) + "[]"; else return elt.getName(); }
From source file:Main.java
/** * Merges two arrays into a new array. Elements from pArray1 and pArray2 will * be copied into a new array, that has pLength1 + pLength2 elements. * * @param pArray1 First array/*ww w. ja v a 2 s . c o m*/ * @param pOffset1 the offset into the first array * @param pLength1 the number of elements to copy from the first array * @param pArray2 Second array, must be compatible with (assignable from) * the first array * @param pOffset2 the offset into the second array * @param pLength2 the number of elements to copy from the second array * @return A new array, containing the values of pArray1 and pArray2. The * array (wrapped as an object), will have the length of pArray1 + * pArray2, and can be safely cast to the type of the pArray1 * parameter. * @see java.lang.System#arraycopy(Object,int,Object,int,int) */ @SuppressWarnings({ "SuspiciousSystemArraycopy" }) public static Object mergeArrays(Object pArray1, int pOffset1, int pLength1, Object pArray2, int pOffset2, int pLength2) { Class class1 = pArray1.getClass(); Class type = class1.getComponentType(); // Create new array of the new length Object array = Array.newInstance(type, pLength1 + pLength2); System.arraycopy(pArray1, pOffset1, array, 0, pLength1); System.arraycopy(pArray2, pOffset2, array, pLength1, pLength2); return array; }
From source file:MainClass.java
static Object doubleArray(Object original) { Object returnValue = null;/*from w w w . ja v a2 s . 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:ArrayGrowTest.java
/** * A convenience method to print all elements in an array * //w w w.ja va2 s.c o 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 . ja v a 2s .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
@SuppressWarnings("unchecked") public static <T> T[] toArray(final Collection<T> c, final Class<? extends T[]> newType) { final int length = c.size(); final T[] array = (T[]) Array.newInstance(newType.getComponentType(), length); if (length > 0) { int i = 0; for (T elem : c) { array[i++] = elem;// w ww .j ava2 s . co m } } return array; }