<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package edu.cornell.cs.cs2110;

/**
 * An array implementation of List.
 * 
 */
public class ArrayList&lt;T&gt; implements List&lt;T&gt; {

   private Object[] theArray; // holds the List elements
   private int empty; // index of next empty array slot

   /**
    * Constructor.
    * 
    * @param capacity
    *           the max size of the List
    */
   public ArrayList(int capacity) {
      theArray = new Object[capacity];
      empty = 0;
   }

   public ArrayList() {
      this(12);
   }

   /**
    * Insert an element onto the list.
    * 
    * @param element
    *           the element to insert
    */
   public void insert(T element) {
      // if beyond end, allocate a new array, copy over old one
      if (empty == theArray.length) {
         Object[] newArray = new Object[theArray.length * 2 + 1];
         System.arraycopy(theArray, 0, newArray, 0, empty);
         theArray = newArray;
      }
      theArray[empty++] = element;
   }
   
   private boolean equal(T x, Object y) {
      return (x != null &amp;&amp; x.equals(y)) || (x == null &amp;&amp; y == null);
   }

   /**
    * Delete an element from the list.
    * 
    * @param element
    *           the element to delete
    */
   public void delete(T element) {
      for (int i = 0; i &lt; empty; i++) {
         if (equal(element, theArray[i])) {
            for (int j = i + 1; j &lt; empty; j++) theArray[j - 1] = theArray[j];
            empty--;
            break;
         }
      }
   }

   /**
    * Report true if list contains element.
    * 
    * @param element
    *           the element to check for
    * @return true iff element is in the List
    */
   public boolean contains(T element) {
      for (int i = 0; i &lt; empty; i++)
         if (equal(element, theArray[i])) return true;
      return false;
   }

   /**
    * Report number of elements in List.
    * 
    * @return number of elements
    */
   public int size() {
      return empty;
   }

   /**
    * String representation.
    * 
    * @return the String representation
    */
   public String toString() {
      String string = "[";
      for (int i = 0; i &lt; empty; i++)
         string += " " + theArray[i].toString();
      string += " ]";
      return string;
   }
}
</pre></body></html>