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
/** * Removes an element from a copy of the array and returns the copy. * If the element is not present, then the original array is returned unmodified. * @param array The original array, or null to represent an empty array. * @param element The element to remove. * @return A new array that contains all of the elements of the original array * except the first copy of the specified element removed. If the specified element * was not present, then returns the original array. Returns null if the result * would be an empty array.// w ww . j av a 2 s. co m */ @SuppressWarnings("unchecked") public static <T> T[] removeElement(Class<T> kind, T[] array, T element) { if (array != null) { final int length = array.length; for (int i = 0; i < length; i++) { if (array[i] == element) { if (length == 1) { return null; } T[] result = (T[]) Array.newInstance(kind, length - 1); System.arraycopy(array, 0, result, 0, i); System.arraycopy(array, i + 1, result, i, length - i - 1); return result; } } } return array; }
From source file:Main.java
/** * Removes an element from a copy of the array and returns the copy. * If the element is not present, then the original array is returned unmodified. * @param array The original array, or null to represent an empty array. * @param element The element to remove. * @return A new array that contains all of the elements of the original array * except the first copy of the specified element removed. If the specified element * was not present, then returns the original array. Returns null if the result * would be an empty array./*from w w w .j a v a 2 s . c o m*/ */ @SuppressWarnings("unchecked") public static <T> T[] removeElement(final Class<T> kind, final T[] array, final T element) { if (array != null) { final int length = array.length; for (int i = 0; i < length; i++) { if (array[i] == element) { if (length == 1) { return null; } final T[] result = (T[]) Array.newInstance(kind, length - 1); System.arraycopy(array, 0, result, 0, i); System.arraycopy(array, i + 1, result, i, length - i - 1); return result; } } } return array; }
From source file:net.firejack.platform.core.utils.ArrayUtils.java
public static <T> T[] getArray(Collection<T> itemList, Class<T> clazz) { return itemList == null || itemList.isEmpty() ? null : itemList.toArray((T[]) Array.newInstance(clazz, itemList.size())); }
From source file:fi.foyt.foursquare.api.JSONFieldParser.java
/** * Static method that parses JSON array into array of FoursquareEntities * /* w w w . j a va2 s .c o m*/ * @param clazz entity class * @param jsonArray JSON Array * @param skipNonExistingFields whether parser should ignore non-existing fields * @return list of entities * @throws FoursquareApiException when something unexpected happens */ public static FoursquareEntity[] parseEntities(Class<?> clazz, JSONArray jsonArray, boolean skipNonExistingFields) throws FoursquareApiException { FoursquareEntity[] result = (FoursquareEntity[]) Array.newInstance(clazz, jsonArray.length()); for (int i = 0, l = jsonArray.length(); i < l; i++) { JSONObject jsonObject; try { jsonObject = jsonArray.getJSONObject(i); result[i] = parseEntity(clazz, jsonObject, skipNonExistingFields); } catch (JSONException e) { throw new FoursquareApiException(e); } } return result; }
From source file:Main.java
/** * Get all child nodes of parent that implement/subclass the given type. * /*from ww w . j a v a2s . c o m*/ * @param parent * Parent to search * @param nodeType * Type of child to search for * @return Array of children of parent that conform to the given nodeType */ public static <N extends Node> N[] getChildrenImplementing(Node parent, Class<N> nodeType) { if (parent == null) { return null; } NodeList children = parent.getChildNodes(); if (children == null) { return null; } int count = 0; for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (nodeType.isAssignableFrom(node.getClass())) { count++; } } @SuppressWarnings("unchecked") N[] array = (N[]) Array.newInstance(nodeType, count); for (int i = 0, pos = 0; i < children.getLength(); i++) { Node node = children.item(i); if (nodeType.isAssignableFrom(node.getClass())) { @SuppressWarnings("unchecked") N n = (N) node; Array.set(array, pos++, n); } } return array; }
From source file:ArrayUtil.java
/** * Copies the elements of the {@code oldArray} to a new array with extra space to * append the given element {@code toAppend1} and the array {@code toAppend2}. *//*from www .j av a 2s . com*/ @SuppressWarnings("unchecked") public static <T> T[] append(T[] oldArray, T toAppend1, T[] toAppend2) { Class<?> component = oldArray.getClass().getComponentType(); T[] array = (T[]) Array.newInstance(component, oldArray.length + 1 + toAppend2.length); System.arraycopy(oldArray, 0, array, 0, oldArray.length); array[oldArray.length] = toAppend1; System.arraycopy(toAppend2, 0, array, oldArray.length + 1, toAppend2.length); return array; }
From source file:Main.java
public static Object arrayExpandAddElements(Object array, Collection elementsToAdd) { Class cl = array.getClass();/*from ww w . j a v a2 s. c o m*/ 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++; } return newArray; }
From source file:Main.java
@SuppressWarnings("rawtypes") private static Object joinArrays(Object paramObject1, Object paramObject2) { Class localClass = paramObject1.getClass().getComponentType(); int i = Array.getLength(paramObject1); int j = i + Array.getLength(paramObject2); Object localObject = Array.newInstance(localClass, j); int k = 0;/*w ww .j a v a 2 s .c o m*/ while (k < i) { Array.set(localObject, k, Array.get(paramObject1, k)); k++; } while (k < j) { Array.set(localObject, k, Array.get(paramObject2, k - i)); k++; } return localObject; }
From source file:Main.java
public static <T> T[] intersection(@NonNull final T[] array1, @NonNull final T[] array2) { final List<T> list1 = new ArrayList<>(); Collections.addAll(list1, array1); final List<T> list2 = new ArrayList<>(); Collections.addAll(list2, array2); list1.retainAll(list2);//w ww .j a v a2s .c o m //noinspection unchecked return list1.toArray((T[]) Array.newInstance(array1.getClass().getComponentType(), list1.size())); }
From source file:ArrayConverter.java
public static Object[] getAsList(final Object maybeArray, final Class arrayType) { if (maybeArray == null) { return null; }//from w w w. ja va 2 s . com if (maybeArray.getClass().isArray() == false) { return new Object[] { maybeArray }; } final ArrayList list = new ArrayList(); ArrayConverter.addToList(list, maybeArray); final Object o = Array.newInstance(arrayType, list.size()); return list.toArray((Object[]) o); }