@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;
      if (newCap - MAX_ARRAY_SIZE > 0)       newCap=hugeCapacity(cap + 1);
      r=Arrays.copyOf(r,newCap);
    }
    r[i++]=(T)it.next();
  }
  return (i == r.length) ? r : Arrays.copyOf(r,i);
}

@ 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 ; if ( newCap - MAX_ARRAY_SIZE > 0 ) newCap = hugeCapacity ( cap + 1 ) ; r = Arrays . copyOf ( r , newCap ) ; } r [ i ++ ] = ( T ) it . next ( ) ; } return ( i == r . length ) ? r : Arrays . copyOf ( r , i ) ; }
reallocates the array being used within toarray when the iterator returned more elements than expected , and finishes filling it from the iterator .
