List of usage examples for java.lang.reflect Array newInstance
public static Object newInstance(Class<?> componentType, int... dimensions) throws IllegalArgumentException, NegativeArraySizeException
From source file:Main.java
public static <T> Object convertListToArray(List<T> list, Class<T> destEntryType) { Object outArray = Array.newInstance(destEntryType, list.size()); int count = 0; int size = list.size(); for (int i = 0; i < size; i++) { Object element = list.get(i); Array.set(outArray, count, element); count++;/*from ww w. j a v a2 s . c om*/ } if (destEntryType.isPrimitive()) return outArray; else return (T[]) outArray; }
From source file:Main.java
/** * Replace the value of a field containing a non null array, by a new array containing the elements of the original * array plus the elements of extraElements. * * @param instance the instance whose field is to be modified. * @param fieldName the field to modify. * @param extraElements elements to append at the end of the array. *///ww w. ja v a2 s. c om static void expandFieldArray(Object instance, String fieldName, Object[] extraElements) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field jlrField = findField(instance, fieldName); Object[] original = (Object[]) jlrField.get(instance); Object[] combined = (Object[]) Array.newInstance(original.getClass().getComponentType(), original.length + extraElements.length); System.arraycopy(extraElements, 0, combined, 0, extraElements.length); System.arraycopy(original, 0, combined, extraElements.length, original.length); jlrField.set(instance, combined); }
From source file:Main.java
@SuppressWarnings("unchecked") public static final <T> T[] add(T[] array, T obj) { if (array == null) { throw new IllegalArgumentException("array"); }// www. j av a 2 s.c om T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length + 1); System.arraycopy(array, 0, newArray, 0, array.length); newArray[array.length] = obj; return newArray; }
From source file:Main.java
@SuppressWarnings("unchecked") public static final <T> T[] remove(T[] array, T obj) { int index = indexOf(array, obj); if (index < 0) { return array; }/*from w w w.j a v a2 s .co m*/ T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length - 1); if (index > 0) { System.arraycopy(array, 0, newArray, 0, index); } if (index < array.length - 1) { System.arraycopy(array, index + 1, newArray, index, newArray.length - index); } return newArray; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <E extends Object> E[] toArray(List<E> list, Class<E> clazz) { return list.toArray((E[]) Array.newInstance(clazz, list.size())); }
From source file:Main.java
@SuppressWarnings("unchecked") private static <E> E[] createArray(Class<E> elemType, int length) { return (E[]) Array.newInstance(elemType, length); }
From source file:Main.java
/** * Remove all null element in the given array. Will return null if the array is null. * * @return A new array if any element is removed, or the origin array if nothing removed. *//*from w w w. j av a 2 s .c om*/ public static <T> T[] removeNull(T[] array) { if (array == null) { return null; } List<T> list = new ArrayList<T>(); boolean changed = false; for (T element : array) { if (element != null) { list.add(element); } else { changed = true; } } if (!changed) { return array; } else { @SuppressWarnings({ "unchecked" }) T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(), list.size()); return list.isEmpty() ? newArray : list.toArray(newArray); } }
From source file:Main.java
public static Object expand(Object obj, int i, boolean flag) { int j = Array.getLength(obj); Object obj1 = Array.newInstance(obj.getClass().getComponentType(), j + i); System.arraycopy(obj, 0, obj1, flag ? 0 : i, j); return obj1;/*from ww w.jav a 2 s . c o m*/ }
From source file:Main.java
public static Object cut(Object obj, int size) { int j;/*from w w w .j a va2 s . c om*/ if ((j = Array.getLength(obj)) == 1) { return Array.newInstance(obj.getClass().getComponentType(), 0); } int k; if ((k = j - size - 1) > 0) { System.arraycopy(obj, size + 1, obj, size, k); } j--; Object obj1 = Array.newInstance(obj.getClass().getComponentType(), j); System.arraycopy(obj, 0, obj1, 0, j); return obj1; }
From source file:Main.java
/** * Converts a collection into an array.//from w ww .j a va2 s .co m * @param collection the collection to convert * @param clazz the class of the items in the collections * @return a non-null array */ public static <T> T[] convertToArray(Collection<T> collection, Class<T> clazz) { T[] result = (T[]) Array.newInstance(clazz, collection.size()); return collection.toArray(result); }