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
/** * 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. jav a 2s. c om * @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:com.dsh105.echopet.compat.api.plugin.ModuleLogger.java
protected static <T> T[] createArray(Class<T> type, int length) { return (T[]) Array.newInstance(type, length); }
From source file:Main.java
public static <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;//from w w w .ja v a2 s. com }
From source file:Main.java
/** * <p>// w w w . j a v a 2 s . com * Removes the element at the specified position from the specified array. All subsequent elements * are shifted to the left (substracts one from their indices). * </p> * * <p> * This method returns a new array with the same elements of the input array except the element on * the specified position. The component type of the returned array is always the same as that of * the input array. * </p> * * <p> * If the input array is <code>null</code>, an IndexOutOfBoundsException will be thrown, because * in that case no valid index can be specified. * </p> * * @param array * the array to remove the element from, may not be <code>null</code> * @param index * the position of the element to be removed * @return A new array containing the existing elements except the element at the specified * position. * @throws IndexOutOfBoundsException * if the index is out of range (index < 0 || index >= array.length), or if the array is * <code>null</code>. * @since 2.1 */ public static <T> T[] remove(final T[] array, final int index) { int length = getLength(array); if (index < 0 || index >= length) { throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length); } Object result = Array.newInstance(array.getClass().getComponentType(), length - 1); System.arraycopy(array, 0, result, 0, index); if (index < length - 1) { System.arraycopy(array, index + 1, result, index, length - index - 1); } return (T[]) result; }
From source file:Main.java
/** * Converts a collection to a primitive array of a given type. * * @param type type of array to create * @param collection collection to convert, must contain items that extend T * @param <T> type of array to create * @return primitive array of type T//from w ww .j a va 2 s. com */ public static <T> T[] toArray(Class<T> type, Collection<? extends T> collection) { T[] array = (T[]) Array.newInstance(type, collection.size()); int i = 0; for (T item : collection) { array[i++] = item; } return array; }
From source file:Main.java
/** * Works on arrys instead of collections * @param array//ww w . jav a 2 s . c om * @param n * @param <T> * @return a list of arrays */ public static <T> List<T[]> split(T[] array, int n) { Class theClass = array[0].getClass(); if (array.length >= n) { @SuppressWarnings("unchecked") List<T[]> out = new ArrayList<>(); int minSegmentSize = (int) Math.floor(array.length / (double) n); int start = 0; int stop = minSegmentSize; for (int i = 0; i < n - 1; i++) { int segmentSize = stop - start; T[] segment = (T[]) Array.newInstance(theClass, segmentSize); int j = 0; for (int k = start; k < stop; k++) { segment[j] = array[k]; j++; } out.add(segment); start = stop; stop += segmentSize; } int segmentSize = array.length - start; stop = start + segmentSize; T[] segment = (T[]) Array.newInstance(theClass, segmentSize); int j = 0; for (int k = start; k < stop; k++) { segment[j] = array[k]; j++; } out.add(segment); return out; } else { throw new IllegalArgumentException("n must not be smaller set size!"); } }
From source file:com.sf.ddao.orm.rsmapper.ArrayRSMapper.java
@Override public Object handle(Context context, ResultSet rs) throws SQLException { Collection list = (Collection) super.handle(context, rs); Object[] array = (Object[]) Array.newInstance(itemType, list.size()); //noinspection unchecked return list.toArray(array); }
From source file:com.sinosoft.one.data.jade.rowmapper.ArrayRowMapper.java
public Object mapRow(ResultSet rs, int rowNum) throws SQLException { int columnSize = rs.getMetaData().getColumnCount(); Object array = Array.newInstance(componentType, columnSize); for (int i = 0; i < columnSize; i++) { Object value = JdbcUtils.getResultSetValue(rs, (i + 1), componentType); Array.set(array, i, value); }// w w w.ja v a 2 s. co m return array; }
From source file:com.gzj.tulip.jade.rowmapper.ArrayRowMapper.java
@Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException { int columnSize = rs.getMetaData().getColumnCount(); Object array = Array.newInstance(componentType, columnSize); for (int i = 0; i < columnSize; i++) { Object value = JdbcUtils.getResultSetValue(rs, (i + 1), componentType); Array.set(array, i, value); }/* w ww .j a va 2s . c o m*/ return array; }
From source file:Main.java
/** * Returns an array of values as a union set of the two input arrays. * * @param <T>//from w w w . j a v a 2 s . c o m * @param values * @param newValues * @return the union of the two arrays */ @Deprecated public static <T> T[] addWithoutDuplicates(T[] values, T[] newValues) { Set<T> originals = new HashSet<>(values.length); for (T value : values) { originals.add(value); } List<T> newOnes = new ArrayList<>(newValues.length); for (T value : newValues) { if (originals.contains(value)) { continue; } newOnes.add(value); } T[] largerOne = (T[]) Array.newInstance(values.getClass().getComponentType(), values.length + newOnes.size()); System.arraycopy(values, 0, largerOne, 0, values.length); for (int i = values.length; i < largerOne.length; i++) { largerOne[i] = newOnes.get(i - values.length); } return largerOne; }