@Override public Object[] convert(Object[] sourceValue) throws ConversionException { Class<Object> componentType = ArrayUtil.componentType(sourceValue); Object[] array = (Object[]) Array.newInstance(componentType, indexes.length); for (int i = 0; i < indexes.length; i++) array[i] = sourceValue[indexes[i]]; return array; }
@SuppressWarnings("unchecked") public static <T> T[] remove(int indexToRemove, T[] array) { Class<T> componentType = componentType(array); T[] result = (T[]) Array.newInstance(componentType, array.length - 1); if (indexToRemove > 0) System.arraycopy(array, 0, result, 0, indexToRemove); System.arraycopy(array, indexToRemove + 1, result, indexToRemove, array.length - indexToRemove - 1); return result; }
public static <T> T[] copyOfRange(T[] array, int offset, int length) { return copyOfRange(array, offset, length, componentType(array)); }
@SuppressWarnings("unchecked") public static <T> T[] append(T value, T[] array) { if (array == null) { return toArray(value); } else { T[] newArray = newInstance(componentType(array), array.length + 1); System.arraycopy(array, 0, newArray, 0, array.length); newArray[array.length] = value; return newArray; } }
public static <T> T[] append(T[] newValues, T[] array) { if (array == null) { return newValues.clone(); } else { T[] newArray = newInstance(componentType(array), array.length + newValues.length); System.arraycopy(array, 0, newArray, 0, array.length); for (int i = 0; i < newValues.length; i++) newArray[array.length + i] = newValues[i]; return newArray; } }
public static <T> T[] cloneAll(T[] input) { T[] output = ArrayUtil.newInstance(ArrayUtil.componentType(input), input.length); for (int i = 0; i < input.length; i++) output[i] = clone(input[i]); return output; }
public static <T> T[] removeAll(T[] toRemove, T[] target) { Class<T> componentType = componentType(target); ArrayBuilder<T> builder = new ArrayBuilder<T>(componentType); for (T element : target) if (!contains(element, toRemove)) builder.add(element); return builder.toArray(); }