<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/**
 * An array implementation of List.
 * 
 * @author Paul Chew for CS211, Sep 2006.
 */
public class ArrayList implements List {
    
    private Object[] theArray;       // Holds the List elements
    private int empty;               // Index of next empty array slot
    
    /**
     * Constructor.
     * @param maxSize the max size of the List
     */
    public ArrayList (int maxSize) {
        theArray = new Object[maxSize];
        empty = 0;
    }
        
    /**
     * Insert an element onto the list.
     * @param element the element to insert
     */
    public void insert (Object element) {
        theArray[empty++] = element;
    }
    
    /**
     * Delete an element from the list.
     * @param element the element to delete
     */
    public void delete (Object element) {
        for (int i = 0; i &lt; empty; i++) {
            if (theArray[i].equals(element)) {
                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 (Object element) {
        for (int i = 0; i &lt; empty; i++)
            if (theArray[i].equals(element)) 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>