/** * Data structure initialization is done here so that child classes can declay initialization until they are ready */ protected void init(int initialMaxSize, Class<T> type, boolean declareInstances) { this.size = 0; this.type = type; this.declareInstances = declareInstances; data = (T[]) Array.newInstance(type, initialMaxSize); if( declareInstances ) { for( int i = 0; i < initialMaxSize; i++ ) { try { data[i] = createInstance(); } catch( RuntimeException e ) { throw new RuntimeException("declareInstances is true, but createInstance() can't create a new instance. Maybe override createInstance()?"); } } } }
/** * Data structure initialization is done here so that child classes can declay initialization until they are ready */ protected void init(int initialMaxSize, Class<T> type, boolean declareInstances) { this.size = 0; this.type = type; this.declareInstances = declareInstances; data = (T[]) Array.newInstance(type, initialMaxSize); if( declareInstances ) { for( int i = 0; i < initialMaxSize; i++ ) { try { data[i] = createInstance(); } catch( RuntimeException e ) { throw new RuntimeException("declareInstances is true, but createInstance() can't create a new instance. Maybe override createInstance()?"); } } } }
/** * Increases the size of the internal array without changing the shape's size. If the array * is already larger than the specified length then nothing is done. Elements previously * stored in the array are copied over is a new internal array is declared. * * @param length Requested size of internal array. */ public void growArray( int length) { // now need to grow since it is already larger if( this.data.length >= length) return; T []data = (T[])Array.newInstance(type, length); System.arraycopy(this.data,0,data,0,this.data.length); if( declareInstances ) { for( int i = this.data.length; i < length; i++ ) { data[i] = createInstance(); } } this.data = data; }
/** * Increases the size of the internal array without changing the shape's size. If the array * is already larger than the specified length then nothing is done. Elements previously * stored in the array are copied over is a new internal array is declared. * * @param length Requested size of internal array. */ public void growArray( int length) { // now need to grow since it is already larger if( this.data.length >= length) return; T []data = (T[])Array.newInstance(type, length); System.arraycopy(this.data,0,data,0,this.data.length); if( declareInstances ) { for( int i = this.data.length; i < length; i++ ) { data[i] = createInstance(); } } this.data = data; }