import java.util.Vector; /** * An implementation of mutable lists that uses vectors. * * @author Samuel A. Rebelsky * @author Yvonne Palm * @author Jonathan Wellons */ public class VectorBasedList implements MutableList { // +-------+--------------------------------------------------- // | Notes | // +-------+ // +-----------+----------------------------------------------- // | Constants | // +-----------+ // +--------+-------------------------------------------------- // | Fields | // +--------+ /** * The actual contents of the list, stored in a Vector. */ Vector contents; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** * Build an empty list. */ public VectorBasedList() { this.contents = new Vector(); } // VectorBasedList() // +------------------+---------------------------------------- // | Exported Methods | // +------------------+ /** * Determine if the list is empty */ public boolean isEmpty() { return this.contents.isEmpty(); } // isEmpty() /** * Update the list by adding an element to the front. */ public void addToFront(Object newThingy) { this.contents.add(0, newThingy); } // addToFront(Object) public void oldAddToFront(Object newThingy) { // Make a new Vector Vector newVector = new Vector(); // Put the new element at the front. newVector.add(newThingy); // Copy everything else over. int numValues = this.contents.size(); for (int i = 0; i < numValues; i++) newVector.add(this.contents.get(i)); // Update this.contents to the new Vector. this.contents = newVector; } // oldAddToFront(Object) /** * Update the list by removing the first element. Return * that element. */ public Object removeFirst() { return this.contents.remove(0); } // removeFirst() private Object oldRemoveFirst() { // Use a for loop to move everything down one. // Grab the first element Object returnMe = this.contents.get(0); int numValues = this.contents.size(); for (int i = 0; i < numValues-1; i++) { this.contents.set(i, this.contents.get(i+1)); } // for // Delete that last element this.contents.setSize(numValues-1); // Return the first element return returnMe; } // removeFirst() /** * Get the length. */ public int length() { return this.contents.size(); } // length() /** * Get the first element (non-destructively). */ public Object getFirst() { return this.contents.get(0); } // getFirst() /** * Convert to a string for ease of printing. */ public String toString() { if (this.contents.isEmpty()) return "()"; String result = "(" + this.contents.get(0); int numValues = this.contents.size(); for (int i = 1; i < numValues; i++) result = result + " " + this.contents.get(i); result = result + ")"; return result; } // toString() /** * Make an independent copy of this list. */ public MutableList copyMe() { return null; // STUB } // copyMe() } // class VectorBasedList