package com.mindprod.example;
import java.util.ArrayList;
import java.util.List;
import static java.lang.System.*;
/**
* Extension to standard ArrayList that automatically grows.
* <p/>
* If you set the sput index value past the current end rather than
* throwing an ArrayIndexOutOfMoundsException, the list automatically grows to accommodate.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2006-02-21
* @since 2006-02-21
*/
public final class GrowingArrayList<E> extends ArrayList<E>
{
/**
* true if want the code for the debugging harness.
*/
private static final boolean DEBUGGING = false;
/**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list.
*
* @throws IllegalArgumentException if the specified initial capacity is negative
*/
@SuppressWarnings( { "SameParameterValue", "WeakerAccess" } )
public GrowingArrayList( int initialCapacity )
{
super( initialCapacity );
}
/**
* Debug harness. Demonstrates use of GrowingArrayList
*
* @param args not used.
*/
public static void main( String[] args )
{
if ( DEBUGGING )
{
List<String> rabbitry = new GrowingArrayList<>( 10 );
rabbitry.add( "rabbit" );
rabbitry.add( "dog" );
rabbitry.add( "buck" );
rabbitry.set( 1, "doe" );
rabbitry.set( 5, "hare" );
for ( String word : rabbitry )
{
out.println( word );
}
}
}
/**
* Replaces the element at the specified position in this list with the specified element. Unlike ordinary
* ArrayList.set the array automatically grows if index >= size.
*
* @param index index of element to replace.
* @param element element to be stored at the specified position.
*
* @return the element previously at the specified position.
* @throws IndexOutOfBoundsException if index out of range <tt>( index < 0 )</tt>.
*/
public E set( int index, E element )
{
int size = this.size();
if ( index < size )
{
return super.set( index, element );
}
int dummiesRequired = index - size;
for ( ; dummiesRequired > 0; dummiesRequired-- )
{
super.add( null );
}
super.add( element );
return null;
}
}