private void grow ( ) { /* Double the size of the topmost array. */ T [ ] [ ] newArrays = ( T [ ] [ ] ) new Object [ mArrays . length * 2 ] [ ] ; /* The new arrays each have size 2^(n + 1). We need 2^(n - 1) of them * to hold the old elements. Allocate those here and copy everything * over. */ for ( int i = 0 ; i < mArrays . length ; i += 2 ) { /* Allocate the array. */ newArrays [ i / 2 ] = ( T [ ] ) new Object [ newArrays . length ] ; /* Use System.arraycopy to move everything over. */ System . arraycopy ( mArrays [ i ] , 0 , newArrays [ i / 2 ] , 0 , mArrays . length ) ; System . arraycopy ( mArrays [ i + 1 ] , 0 , newArrays [ i / 2 ] , mArrays . length , mArrays . length ) ; /* Null out the old arrays to be nice to the GC during this * potentially stressful time. */ mArrays [ i ] = mArrays [ i + 1 ] = null ; } /* Switch out this new array for the old. */ mArrays = newArrays ; /* Bump up lg2 of the size. */ ++ mLgSize ; }
Grows the internal representation by doubling the size of the topmost array and copying the appropriate number of elements over .