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
@SuppressWarnings("unchecked") public static final <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { T[] copy = ((Object) newType == (Object) Object[].class) ? (T[]) new Object[newLength] : (T[]) Array.newInstance(newType.getComponentType(), newLength); System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy;// w ww . j av a2 s.c o m }
From source file:ArrayUtil.java
/** * Add array./* w w w . j a v a 2s .c o m*/ * * @param <T> * @param from * @param to * @return */ @SuppressWarnings("unchecked") public static <T> T[] addAll(T[] from, T[] to) { T[] newone = (T[]) Array.newInstance(from.getClass().getComponentType(), from.length + to.length); copyAll(from, newone); copy(to, newone, 0, from.length, to.length); return newone; }
From source file:Main.java
/** * Converts Collection to array. /* w w w.jav a 2 s .c o m*/ * @param <T> * @param col * @param cls * @return * @see java.util.Collection#toArray(T[]) */ public static <T> T[] toArray(Collection<T> col, Class<T> cls) { T[] arr = (T[]) Array.newInstance(cls, col.size()); return col.toArray(arr); }
From source file:Main.java
@SuppressWarnings("unchecked") public static <E extends Object> E[] toArray(Set<E> set, Class<E> clazz) { return set.toArray((E[]) Array.newInstance(clazz, set.size())); }
From source file:Main.java
/** * Converts a typed {@link Collection} to a typed array. * /*from w ww . j a va2s . com*/ * @param type * the type class * @param collection * the collection to convert to an array. * @return the array. */ @SuppressWarnings("unchecked") public static <T> T[] toArray(Class<T> type, Collection<T> collection) { return collection.toArray((T[]) Array.newInstance(type, collection.size())); }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T[] toArray(List<?> list, Class<T> type) { T[] array = (T[]) Array.newInstance(type, list.size()); return list.toArray(array); }
From source file:ArrayUtil.java
/** * Add new value to exsisting array.The new value is indexed to the last. * /*from w w w . ja v a 2 s . c o m*/ * @param <T> * @param current * @param value * @return */ @SuppressWarnings("unchecked") public static <T> T[] add(T[] current, T value) { T[] newone = (T[]) Array.newInstance(current.getClass().getComponentType(), current.length + 1); copyAll(current, newone); newone[current.length] = value; return newone; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T[] grow(T[] array, int length, T fillElement) { Class<T> componentType = (Class<T>) array.getClass().getComponentType(); T[] newArray = (T[]) Array.newInstance(componentType, length); System.arraycopy(array, 0, newArray, 0, array.length); Arrays.fill(newArray, array.length, newArray.length, fillElement); return newArray; }
From source file:ArrayUtil.java
/** * Add new value, which sets as the first value, to existing array. * /*ww w . java 2 s.co m*/ * @param <T> * @param current * @param value * @return */ @SuppressWarnings("unchecked") public static <T> T[] addFirst(T[] current, T value) { T[] newone = (T[]) Array.newInstance(current.getClass().getComponentType(), current.length + 1); copy(current, newone, 0, 1, current.length); newone[0] = value; return newone; }
From source file:Main.java
public static Object arrayExpandAddSingle(Object array, Object elementsToAdd) { Class cl = array.getClass();/*from www . j a v a2 s . c om*/ 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; }