
public class MyArrayList<AnyType> implements Iterable<AnyType>
{
    /**
     * Construct an empty ArrayList.
     */
    public MyArrayList( )
    {
        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 java.util.Iterator<AnyType> iterator( )
    {
        return new ArrayListIterator<AnyType>( this );
    }

	/**
	 * Returns a String representation of this collection.
	 */
	public String toString( )
	{
		StringBuilder sb = new StringBuilder( "[ " );
		
		for( AnyType x : this )
			sb.append( x + " " );
		sb.append( "]" );
	
		return new String( sb );
	}
	
    
    private static final int DEFAULT_CAPACITY = 10;
    
    public AnyType [ ] theItems;
    private int theSize;
}



    /**
     * This is the implementation of the ArrayListIterator.
     * It maintains a notion of a current position and of
     * course the implicit reference to the MyArrayList.
     */
    class ArrayListIterator<AnyType> implements java.util.Iterator<AnyType>
    {
        private int current ;
	private boolean okToRemove;
        private MyArrayList<AnyType> theList;

        public ArrayListIterator( MyArrayList<AnyType> 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;
        }
    }





























class TestArrayList
{
	public static void main( String [ ] args )
	{
		MyArrayList<Integer> lst = new MyArrayList<Integer>( );
		
		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 );

                java.util.Iterator<Integer> itr = lst.iterator();

                while (itr.hasNext()) {
		    if (  (itr.next() % 2) == 0 ) itr.remove();
                }

		
		System.out.println( lst );
	}
}