src/main/java/org/organicdesign/fp/collections/Cowry.java
public static <T> T[] insertIntoArrayAt(T item, T[] items, int idx, Class<T> tClass) {
        // Make an array that's one bigger.  It's too bad that the JVM bothers to
        // initialize this with nulls.
        @SuppressWarnings("unchecked")
        T[] newItems = (T[]) ((tClass == null) ? new Object[items.length + 1]
                                               : Array.newInstance(tClass, items.length + 1) );

        // If we aren't inserting at the first item, array-copy the items before the insert
        // point.
        if (idx > 0) {
            System.arraycopy(items, 0, newItems, 0, idx);
        }

        // Insert the new item.
        newItems[idx] = item;

        // If we aren't inserting at the last item, array-copy the items after the insert
        // point.
        if (idx < items.length) {
            System.arraycopy(items, idx, newItems, idx + 1, items.length - idx);
        }

        return newItems;
    }
public static <T> T[] insertIntoArrayAt(T item, T[] items, int idx, Class<T> tClass) {
        // Make an array that's one bigger.  It's too bad that the JVM bothers to
        // initialize this with nulls.
        @SuppressWarnings("unchecked")
        T[] newItems = (T[]) ((tClass == null) ? new Object[items.length + 1]
                                               : Array.newInstance(tClass, items.length + 1) );

        // If we aren't inserting at the first item, array-copy the items before the insert
        // point.
        if (idx > 0) {
            System.arraycopy(items, 0, newItems, 0, idx);
        }

        // Insert the new item.
        newItems[idx] = item;

        // If we aren't inserting at the last item, array-copy the items after the insert
        // point.
        if (idx < items.length) {
            System.arraycopy(items, idx, newItems, idx + 1, items.length - idx);
        }

        return newItems;
    }
public static < T > T [ ] insertIntoArrayAt ( T item , T [ ] items , int idx , Class < T > tClass ) { // Make an array that's one bigger.  It's too bad that the JVM bothers to // initialize this with nulls. @ SuppressWarnings ( "unchecked" ) T [ ] newItems = ( T [ ] ) ( ( tClass == null ) ? new Object [ items . length + 1 ] : Array . newInstance ( tClass , items . length + 1 ) ) ; // If we aren't inserting at the first item, array-copy the items before the insert // point. if ( idx > 0 ) { System . arraycopy ( items , 0 , newItems , 0 , idx ) ; } // Insert the new item. newItems [ idx ] = item ; // If we aren't inserting at the last item, array-copy the items after the insert // point. if ( idx < items . length ) { System . arraycopy ( items , idx , newItems , idx + 1 , items . length - idx ) ; } return newItems ; }
Returns a new array one longer than the given one, with the specified item inserted at the specified index.
@param item The item to insert
@param items The original array (will not be modified)
@param idx the index to insert the item at.
@param tClass The runtime class to store in the new array (or null for an Object array).
@return A copy of the given array with the additional item at the appropriate index
(will be one longer than the original array).
Returns a new array one longer than the given one with the specified item inserted at the specified index .