@ SuppressWarnings ( "unchecked" ) public static < T > T [ ] cloneArray ( T [ ] original ) { if ( original == null ) { return null ; } try { Class < ? > clazz = original . getClass ( ) . getComponentType ( ) ; T [ ] copy = ( T [ ] ) Array . newInstance ( clazz , original . length ) ; if ( copy . length != 0 ) { Constructor < T > copyConstructor = ( Constructor < T > ) clazz . getConstructor ( clazz ) ; int i = 0 ; for ( T t : original ) { copy [ i ++ ] = copyConstructor . newInstance ( t ) ; } } return copy ; } catch ( Exception e ) { throw new RuntimeException ( "Cannot copy array. Does its type have a copy-constructor? " + original , e ) ; } }
Performs a deep copy of the given array by calling a copy constructor to build clones of each array element .