/**
 * An implementation of "expandable" arrays.  Intented to help us
 * better understand java.util.Vector (and to give us confidence that
 * we could write that class if we had to).
 *
 * @author Yvonne Palm
 * @author Samuel A. Rebelsky
 * @version 1.0 of September 2003
 */
public class MyVector
{
  // +-----------+-----------------------------------------------
  // | Constants |
  // +-----------+

  /**
   * The default starting capacity.
   */
  public static final int INITIAL_CAPACITY = 5;


  // +--------+--------------------------------------------------
  // | Fields |
  // +--------+

  /** 
   * The contents of the vector.
   */
  Object[] contents;

  /**
   * The number of elements in the vector.
   */
  int length;

  // +--------------+--------------------------------------------
  // | Constructors |
  // +--------------+

  /** Build a new vector. */
  public MyVector()
  {
    this.contents = new Object[INITIAL_CAPACITY];
    this.length = 0;
  } // MyVector()

  // +------------------+----------------------------------------
  // | Exported Methods |
  // +------------------+

  /** Add an element to the end of the vector. */
  public void add(Object newValue)
  {
    // Make sure there's enough space!
    if (this.contents.length == this.length) {
      // Create a bigger array.
      Object[] temp = new Object[this.contents.length + 1];
      // Copy the elements into the new array.
      for (int i = 0; i < this.contents.length; i++) {
        temp[i] = this.contents[i];
      } // for
      // Make contents refer to this array
      this.contents = temp;
    }
    // Put the object in the length position
    this.contents[length] = newValue;
    // The length increases by one.  EXPENSIVE
    this.length++;
    } // add(Object)

  /** 
   * Set an element within the vector. 
   * Set any undefined elements to 0.
   */
  public void set(int pos, Object value){
    if (this.length < (pos + 1)) {
      //System.out.println("Length New: " + (pos+1));
      Object[] temp = new Object[pos + 1];
      // Copy the elements into the new array.
      for (int i = 0; i < this.length; i++) {
        temp[i] = this.contents[i];
      } // for (i less than present length)
      for (int i = (this.length); i < pos; i++){
        temp[i] = null;  
      }//for (i greater than present length)
      temp[pos] = value;
      // Make contents refer to this array
      this.contents = temp;
      length = pos+1;
       }//if (the current vector is not long enough to contain an object in position, pos)
    else if (this.length > (pos + 1)){ // EXTRANEOUS TEST
      this.contents[pos] = value; 
    }
   } // set(int, Object)

  /** Get an element at a specified position. */
  public Object get(int pos)
  {
    if ((pos >= 0) && (pos < this.contents.length)) {
     return this.contents[pos];
   }
   else {
     return null;
   }
  } // get(int)

  /** Determine the length of a vector. */
  public int length()
  {
    return this.length;  
  } // length()

  /** Convert this vector to a string for testing. */
  public String toString()
  {
    if (0 == this.length) { return "[]"; }
    String result = "[" + this.contents[0];
    for (int i = 1; i < this.length; i++) {
      result = result + "," + this.contents[i];
    }
    return result + "]";
  } // toString()

  /** 
   * Remove the last element in the vector.
   */
 public void subtract()
  {
/*
      Object[] temp = new Object[this.contents.length - 1];
      // Copy the elements into the new array.
      for (int i = 0; i < this.contents.length-1; i++) {
        temp[i] = this.contents[i];
      } // for
      // Make contents refer to this array
      this.contents = temp;
      this.length--;
 */
      this.length 
  } // subtract

  public void remove(int pos)
  {
      Object[] temp = new Object[this.contents.length - 1];
      // Copy the elements into the new array.
      for (int i = 0; i < pos; i++) {
        temp[i] = this.contents[i];
      } // for (i less than pos)
      for (int i = (pos); i < (this.contents.length-1); i++) {
          temp[i] = this.contents[i-1];
      }//for (i >= pos)
      // Make contents refer to this array
      this.contents = temp;
      this.length--;
  } // remove

public void removeRange(int pos1, int pos2)
  {
      Object[] temp = new Object[this.contents.length - (pos2 - pos1 + 1)];
      // Copy the elements into the new array.
      for (int i = 0; i < pos1; i++) {
        temp[i] = this.contents[i];
      } // for (i less than pos1)
      for (int i = pos1; i < temp.length; i++) {
          temp[i] = this.contents[i + (pos2 - pos1 + 1)];
      }//for (i >= pos1)
      // Make contents refer to this array
      this.contents = temp;
      this.length = this.length - (pos2 - pos1 +1);
  } // removeRange
  // +---------------+-------------------------------------------
  // | Local Methods |
  // +---------------+

  private void expandContents(int newSize)
  {
  } // expandContents(int)

} // class MyVector

