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 .