public static < A , O extends A > A [ ] addObjectToArray ( A [ ] array , O obj ) { Class < ? > compType = Object . class ; if ( array != null ) { compType = array . getClass ( ) . getComponentType ( ) ; } else if ( obj != null ) { compType = obj . getClass ( ) ; } int newArrLength = ( array != null ? array . length + 1 : 1 ) ; @ SuppressWarnings ( "unchecked" ) A [ ] newArr = ( A [ ] ) Array . newInstance ( compType , newArrLength ) ; if ( array != null ) { System . arraycopy ( array , 0 , newArr , 0 , array . length ) ; } newArr [ newArr . length - 1 ] = obj ; return newArr ; }
Append the given object to the given array returning a new array consisting of the input array contents plus the given object .