package rebelsky.list; /** * Simple cursors for array-based simple lists. * * @author CSC152 2004F */ class ArrayBasedSimpleCursor implements SimpleCursor { // +--------+------------------------------------------------------------ // | Fields | // +--------+ /** * The position in the array. */ int p; /** * The list that we iterate. */ ArrayBasedSimpleList list; // +--------------+------------------------------------------------------ // | Constructors | // +--------------+ /** * Construct the cursor. */ public ArrayBasedSimpleCursor(ArrayBasedSimpleList _list) { this.p = 0; this.list = _list; } // ArrayBasedSimpleCursor // +---------+----------------------------------------------------------- // | Methods | // +---------+ /** * Get a value not yet visited by this cursor. See * interface for more details. */ public Object next() throws NoSuchPosition { // Make sure that it's a valid position. if (!this.hasNext()) throw new NoSuchPosition("Can't get position " + this.p + " in an " + this.list.n + " element list "); // Look it up in the array. Object tmp = this.list.contents[this.p]; // Add one to p to move to the next place. ++this.p; // That's it. return tmp; } // next() /** * Determine whether or not unvisited elements remain. * See interface for more details. */ public boolean hasNext() { return this.p < this.list.n; } // hasNext() } // class ArrayBasedSimpleCursor