/** * Removes an item from the array. * * If the item has been found, a new array is returned where this item is removed. Otherwise the original array is returned. * * @param src the src array * @param object the object to remove * @param <T> the type of the array * @return the resulting array */ public static <T> T[] remove(T[] src, T object) { int index = indexOf(src, object); if (index == -1) { return src; } T[] dst = (T[]) Array.newInstance(src.getClass().getComponentType(), src.length - 1); System.arraycopy(src, 0, dst, 0, index); if (index < src.length - 1) { System.arraycopy(src, index + 1, dst, index, src.length - index - 1); } return dst; }
/** * Removes an item from the array. * * If the item has been found, a new array is returned where this item is removed. Otherwise the original array is returned. * * @param src the src array * @param object the object to remove * @param <T> the type of the array * @return the resulting array */ public static <T> T[] remove(T[] src, T object) { int index = indexOf(src, object); if (index == -1) { return src; } T[] dst = (T[]) Array.newInstance(src.getClass().getComponentType(), src.length - 1); System.arraycopy(src, 0, dst, 0, index); if (index < src.length - 1) { System.arraycopy(src, index + 1, dst, index, src.length - index - 1); } return dst; }
/** * Replaces the first occurrence of the oldValue by the newValue. * * If the item is found, a new array is returned. Otherwise the original array is returned. * * @param src * @param oldValue the value to look for * @param newValues the value that is inserted. * @param <T> the type of the array * @return */ public static <T> T[] replaceFirst(T[] src, T oldValue, T[] newValues) { int index = indexOf(src, oldValue); if (index == -1) { return src; } T[] dst = (T[]) Array.newInstance(src.getClass().getComponentType(), src.length - 1 + newValues.length); // copy the first part till the match System.arraycopy(src, 0, dst, 0, index); // copy the second part from the match System.arraycopy(src, index + 1, dst, index + newValues.length, src.length - index - 1); // copy the newValues into the dst System.arraycopy(newValues, 0, dst, index, newValues.length); return dst; }
/** * Replaces the first occurrence of the oldValue by the newValue. * * If the item is found, a new array is returned. Otherwise the original array is returned. * * @param src * @param oldValue the value to look for * @param newValues the value that is inserted. * @param <T> the type of the array * @return */ public static <T> T[] replaceFirst(T[] src, T oldValue, T[] newValues) { int index = indexOf(src, oldValue); if (index == -1) { return src; } T[] dst = (T[]) Array.newInstance(src.getClass().getComponentType(), src.length - 1 + newValues.length); // copy the first part till the match System.arraycopy(src, 0, dst, 0, index); // copy the second part from the match System.arraycopy(src, index + 1, dst, index + newValues.length, src.length - index - 1); // copy the newValues into the dst System.arraycopy(newValues, 0, dst, index, newValues.length); return dst; }