import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * Description: 
 * 
 * @author Glenn Lancaster
 * @version 15 May 07
 */
public class MyVector
{
  private Object[] arr;
  private int sz;

  
  public MyVector()
  {
    sz = 0;
    arr = new Object[10];
  }

  
  private void ensure(int newSize)
  {
    if ( newSize <= sz ) {
      return;
    }
    Object[] tmp = new Object[newSize];
    for(int i = 0; i < sz; i++) {
      tmp[i] = arr[i];
    }
    arr = tmp;
  }
  
  public void add(Object x)
  {
    // precondition: sz < capacity
    if ( sz >= arr.length ) {
      ensure( 2 * sz );
    }
    arr[sz] = x;
    sz++;
  }
  
  public int size()
  {
    return sz;
  }

  // New methods 5/22/207
  
  public void set(int index, Object elem)
  {
    if ( index < 0 || index >= sz ) {
      throw new IndexOutOfBoundsException();
    }
    arr[index] = elem;
  }

  
  public Object get(int index)
  {
    if ( index < 0 || index >= sz ) {
      throw new IndexOutOfBoundsException();
    }
    return arr[index];
  }
  
  public Iterator iterator()
  {
    return new MIterator();
  }

  // Inner class to implement an Iterator for MyVector
  private class MIterator implements Iterator
  {
    private int pos;

    
    public MIterator()
    {
      pos = 0;
    }
    public boolean hasNext()
    {
      return pos < sz;
    }

    public Object next()
    {
      // Precondition: hasNext() should be true
      if ( !hasNext() ) {
	throw new NoSuchElementException();
      }
      int ix = pos;
      pos++;
      return arr[ix];
    }

    
    public void remove()
    {
      throw new UnsupportedOperationException();
    }

  }
				     
}