import java.util.Vector; /** * An implementation of mutable lists that uses vectors. * the implementationa and the interface is different. The vector is stored and implemented backwards. * * @author Samuel A. Rebelsky * @author Yvonne Palm * @author Jonathan Wellons */ public class ReversedVectorBasedList implements MutableList { // +-------+--------------------------------------------------- // | Notes | // +-------+ // +-----------+----------------------------------------------- // | Constants | // +-----------+ // +--------+-------------------------------------------------- // | Fields | // +--------+ /** * The actual contents of the list, stored in a Vector. */ Vector contents; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** * Build an empty list. */ public ReversedVectorBasedList() { this.contents = new Vector(); } // ReversedVectorBasedList() // +------------------+---------------------------------------- // | Exported Methods | // +------------------+ /** * Determine if the list is empty */ public boolean isEmpty() { return this.contents.isEmpty(); } // isEmpty() /** * Update the vector by adding an element to the back. */ public void addToFront(Object newThingy) { this.contents.add(newThingy); } // addToFront(Object) /** * Update the list by removing the last element in the vector. Return * that element. */ public Object removeFirst() { return this.contents.remove(last()); } // removeFirst() // Find the position of the last element in the vector public int last(){ if (this.contents.size() >= 1) return (this.contents.size() - 1); return (this.contents.size() - 1); }//last() /** * Get the length. */ public int length() { return this.contents.size(); } // length() /** * Get the first element of the list (non-destructively) by getting the last element of the vector. */ public Object getFirst() { return this.contents.get(last()); } // getFirst() /** * Convert to a string for ease of printing. */ public String toString() { if (this.contents.isEmpty()) return "()"; String result = "(" + this.contents.get(last()); for (int i = last()-1; i >= 0; 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 ReversedVectorBasedList