Java tutorial
//package com.java2s; import java.lang.reflect.Array; public class Main { /** * Returns an array containing the elements of parameter source, with one * element removed or added. Parameter adjust {-1, +1} indicates the * operation. Parameter colindex indicates the position at which an element * is removed or added. Parameter addition is an Object to add when * adjust is +1. */ public static Object toAdjustedArray(Object source, Object addition, int colindex, int adjust) { int newsize = Array.getLength(source) + adjust; Object newarray = Array.newInstance(source.getClass().getComponentType(), newsize); copyAdjustArray(source, newarray, addition, colindex, adjust); return newarray; } /** * Copies elements of source to dest. If adjust is -1 the element at * colindex is not copied. If adjust is +1 that element is filled with * the Object addition. All the rest of the elements in source are * shifted left or right accordingly when they are copied. If adjust is 0 * the addition is copied over the element at colindex. * * No checks are perfomed on array sizes and an exception is thrown * if they are not consistent with the other arguments. */ public static void copyAdjustArray(Object source, Object dest, Object addition, int colindex, int adjust) { int length = Array.getLength(source); if (colindex < 0) { System.arraycopy(source, 0, dest, 0, length); return; } System.arraycopy(source, 0, dest, 0, colindex); if (adjust == 0) { int endcount = length - colindex - 1; Array.set(dest, colindex, addition); if (endcount > 0) { System.arraycopy(source, colindex + 1, dest, colindex + 1, endcount); } } else if (adjust < 0) { int endcount = length - colindex - 1; if (endcount > 0) { System.arraycopy(source, colindex + 1, dest, colindex, endcount); } } else { int endcount = length - colindex; Array.set(dest, colindex, addition); if (endcount > 0) { System.arraycopy(source, colindex, dest, colindex + 1, endcount); } } } }