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 Object[] toArrayOnListOfSameType(List aList) { if (aList.isEmpty()) return new Object[0]; Class type = aList.get(0).getClass(); Object[] sameElementsArray = (Object[]) Array.newInstance(type, aList.size()); return aList.toArray(sameElementsArray); }
From source file:Main.java
public static <T extends Object> T[] extendModel(ListModel selected, T[] extras, Class<T> type) { int selectedSize = selected.getSize(); int extraSize = extras.length; @SuppressWarnings("unchecked") T[] augmented = (T[]) Array.newInstance(type, selectedSize + extraSize); // copy current for (int i = 0; i < selectedSize; i++) { augmented[i] = type.cast(selected.getElementAt(i)); }/*www. jav a2 s . c om*/ // augment for (int i = 0; i < extraSize; i++) { augmented[selectedSize + i] = extras[i]; } return augmented; }
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 ww. ja v a 2 s .c o 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 <T> T[] join(T[] head, T[] tail) { if (head == null) { return tail; }// w w w. ja va2 s. com if (tail == null) { return head; } Class<?> type = head.getClass().getComponentType(); T[] result = (T[]) Array.newInstance(type, head.length + tail.length); System.arraycopy(head, 0, result, 0, head.length); System.arraycopy(tail, 0, result, head.length, tail.length); return result; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T[] toArray(List<T> items, Class<T> tClass) { if (items == null || items.size() == 0) return null; int size = items.size(); try {// w w w . j a v a 2s. c om T[] array = (T[]) Array.newInstance(tClass, size); return items.toArray(array); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
/** * /*from w ww .j av a 2s. c om*/ * @param src * @param dst * @return */ @SuppressWarnings("unchecked") public static <T> T[] instanceArray(T src[], T dst[]) { T ret[] = null; if (src != null) { if (dst == null || dst.length != src.length) { dst = (T[]) Array.newInstance(src.getClass().getComponentType(), src.length); } ret = dst; System.arraycopy(src, 0, ret, 0, src.length); } return ret; }
From source file:Main.java
public static <T extends Object> T[] addItem(T[] array, T item, int index) { int size = array == null ? 0 : array.length; if (index < 0 || index > size) index = size;/*from ww w . ja v a 2 s. c om*/ @SuppressWarnings("unchecked") T[] result = (T[]) Array.newInstance(item.getClass(), size + 1); if (index > 0 && array != null) System.arraycopy(array, 0, result, 0, index); result[index] = item; if (index < result.length - 1 && array != null) System.arraycopy(array, index, result, index + 1, array.length - index); return result; }
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 a 2 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:Main.java
public static Object expand(Object a) { Class cl = a.getClass();/*from www. j a v a 2s . c o 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
/** * Produces a copy of the given array;/*from w w w. j a v a 2 s . c o m*/ * @param array the array to copy. * @param <T> Any type * @return the copy of the given array; */ public static <T> T[] duplicateArray(T[] array) { @SuppressWarnings("unchecked") T[] copy = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length); System.arraycopy(array, 0, copy, 0, array.length); return copy; }