@ SuppressWarnings ( "unchecked" ) private static < T > T [ ] finishToArray ( T [ ] r , Iterator < ? > it ) { int i = r . length ; while ( it . hasNext ( ) ) { int cap = r . length ; if ( i == cap ) { int newCap = cap + ( cap >> 1 ) + 1 ; // overflow-conscious code if ( newCap - MAX_ARRAY_SIZE > 0 ) newCap = hugeCapacity ( cap + 1 ) ; r = Arrays . copyOf ( r , newCap ) ; } r [ i ++ ] = ( T ) it . next ( ) ; } // trim if overallocated return ( i == r . length ) ? r : Arrays . copyOf ( r , i ) ; }
Copied from AbstractCollection to support toArray methods