public class HWArrayList { /** * Construct an empty ArrayList. */ public HWArrayList( ) { clear( ); } /** * Returns the number of items in this collection. * @return the number of items in this collection. */ public int size( ) { return theSize; } /** * Returns true if this collection is empty. * @return true if this collection is empty. */ public boolean isEmpty( ) { return size( ) == 0; } /** * Returns the item at position idx. * @param idx the index to search in. * @throws ArrayIndexOutOfBoundsException if index is out of range. */ public AnyType get( int idx ) { if( idx < 0 || idx >= size( ) ) throw new ArrayIndexOutOfBoundsException( "Index " + idx + "; size " + size( ) ); return theItems[ idx ]; } /** * Changes the item at position idx. * @param idx the index to change. * @param newVal the new value. * @return the old value. * @throws ArrayIndexOutOfBoundsException if index is out of range. */ public AnyType set( int idx, AnyType newVal ) { if( idx < 0 || idx >= size( ) ) throw new ArrayIndexOutOfBoundsException( "Index " + idx + "; size " + size( ) ); AnyType old = theItems[ idx ]; theItems[ idx ] = newVal; return old; } public void ensureCapacity( int newCapacity ) { if( newCapacity < theSize ) return; AnyType [ ] old = theItems; theItems = (AnyType []) new Object[ newCapacity ]; for( int i = 0; i < size( ); i++ ) theItems[ i ] = old[ i ]; } /** * Adds an item to this collection, at the end. * @param x any object. * @return true. */ public boolean add( AnyType x ) { add( size( ), x ); return true; } /** * Adds an item to this collection, at the specified index. * @param x any object. * @return true. */ public void add( int idx, AnyType x ) { if( theItems.length == size( ) ) ensureCapacity( size( ) * 2 + 1 ); for( int i = theSize; i > idx; i-- ) theItems[ i ] = theItems[ i - 1 ]; theItems[ idx ] = x; theSize++; } /** * Removes an item from this collection. * @param idx the index of the object. * @return the item was removed from the collection. */ public AnyType remove( int idx ) { AnyType removedItem = theItems[ idx ]; for( int i = idx; i < size( ) - 1; i++ ) theItems[ i ] = theItems[ i + 1 ]; theSize--; return removedItem; } /** * Change the size of this collection to zero. */ public void clear( ) { theSize = 0; ensureCapacity( DEFAULT_CAPACITY ); } /** * Obtains an Iterator object used to traverse the collection. * @return an iterator positioned prior to the first element. */ public ArrayListIterator iterator( ) { return new ArrayListIterator( this ); } /** * Returns a String representation of this collection. */ private static final int DEFAULT_CAPACITY = 10; public AnyType [ ] theItems; private int theSize; public static void main( String [ ] args ) { HWArrayList lst = new HWArrayList( ); for( int i = 0; i < 10; i++ ) lst.add( i ); for( int i = 20; i < 30; i++ ) lst.add( 0, i ); lst.remove( 0 ); lst.remove( lst.size( ) - 1 ); ArrayListIterator itr = lst.iterator(); while (itr.hasNext()) { if ( (itr.next() % 2) == 0 ) itr.remove(); } ArrayListIterator itr1 = lst.iterator(); while (itr1.hasNext()) { System.out.println(itr1.next()); } } } /** * This is the implementation of the ArrayListIterator. * It maintains a notion of a current position and of * course the implicit reference to the HWArrayList. */ class ArrayListIterator implements java.util.Iterator { private int current ; private boolean okToRemove; private HWArrayList theList; public ArrayListIterator( HWArrayList list ) { theList = list; current = 0; okToRemove = false; } public boolean hasNext( ) { return current < theList.size( ); } public AnyType next( ) { if( !hasNext( ) ) throw new java.util.NoSuchElementException( ); okToRemove = true; return theList.theItems[ current++ ]; } public void remove( ) { if( !okToRemove ) throw new IllegalStateException( ); theList.remove( --current ); okToRemove = false; } }