public class ArraySequence extends Sequence
{ protected Object[] a;
  protected int capacity=16;
  // INVARIANT: a[i] != null for 0 <= i < length;
  
  public ArraySequence()
  { a = new Object[capacity];
  }
  
  public void append(Object object)
  { if (length==capacity) // double the capacity
    { Object[] tmp = a;
      capacity *= 2;
      a = new Object[capacity];
      for (int i=0; i<length; i++)
        a[i] = tmp[i];
    }
    a[length++] = object;
  }
  
  public Object get(int index)
  { if (index >= length) return null;
    return a[index];
  }
  
  public Object remove(int index)
  { if (index >= length) return null;
    Object x = a[index];
    for (int i=index; i<length; i++)
      a[i] = a[i+1];
    --length;
    return x;
  }
  
  public boolean remove(Object object)
  { int i=indexOf(object);
    if (i == -1) return false;
    remove(i);
    return true;
  }
  
  public Object set(int index, Object object)
  { if (index >= length) return null;
    Object x = a[index];
    a[index] = object;
    return x;
  }  
}