@ SuppressWarnings ( "unchecked" ) public static < T > T [ ] insert ( T element , T [ ] array , int index ) { Assert . notNull ( array , "Array is required" ) ; assertThat ( index ) . throwing ( new ArrayIndexOutOfBoundsException ( String . format ( "[%1$d] is not a valid index [0, %2$d] in the array" , index , array . length ) ) ) . isGreaterThanEqualToAndLessThanEqualTo ( 0 , array . length ) ; Class < ? > componentType = array . getClass ( ) . getComponentType ( ) ; componentType = defaultIfNull ( componentType , ObjectUtils . getClass ( element ) ) ; componentType = defaultIfNull ( componentType , Object . class ) ; T [ ] newArray = ( T [ ] ) Array . newInstance ( componentType , array . length + 1 ) ; if ( index > 0 ) { System . arraycopy ( array , 0 , newArray , 0 , index ) ; } newArray [ index ] = element ; if ( index < array . length ) { System . arraycopy ( array , index , newArray , index + 1 , array . length - index ) ; } return newArray ; }
Inserts element into the array at index .