Computer Science 210
Data Structures
Fall 2016, Siena College
Iterators BlueJ Project
Click here to download a BlueJ project for Iterators.
Iterators Source Code
The Java source code for Iterators is below. Click on a file name to download it.
import structure5.*; import java.util.Iterator; /** Testing class for Iterator examples @version $Id: Iterators.java 950 2009-10-07 02:09:52Z terescoj $ @author Jim Teresco, 2009 */ public class Iterators { public static final int SIZE = 10; public static void main(String args[]) { // create a vector of integers for testing Vector<Integer> v = new Vector<Integer>(SIZE); for (int i=0; i<SIZE; i++) { v.add(i); } // traverse with the standard iterator System.out.println("Standard iterator:"); Iterator<Integer> vi = v.iterator(); while (vi.hasNext()) { System.out.println(vi.next()); } // traverse with even-only iterator System.out.println("Even-only iterator:"); vi = new VectorEvenIterator<Integer>(v); while (vi.hasNext()) { System.out.println(vi.next()); } // traverse with prefix sum iterator System.out.println("Prefix sum iterator:"); vi = new PrefixSumIterator(v); while (vi.hasNext()) { System.out.println(vi.next()); } } }
import structure5.*; /** * An iterator class for implementing an iterator over a Vector * of numbers that returns the prefix sum of the numbers rather * than the numbers themselves. That is, the sum of all values * up to and include the current. * * @version $Id: PrefixSumIterator.java 950 2009-10-07 02:09:52Z terescoj $ * @author, Jim Teresco, 2009 */ public class PrefixSumIterator extends AbstractIterator<Integer> { /** * The associated vector */ protected Vector<Integer> theVector; /** * The index of the current value. */ protected int current; /** The prefix sum of the items encountered so far. */ protected int prefixSum; /** * Construct a vector iterator to traverse vector v * * @post constructs an initialized iterator associated with v * * @param v The underlying vector. */ public PrefixSumIterator(Vector<Integer> v) { theVector = v; reset(); } /** * Reset the vector iterator to the first value in the vector. * * @post the iterator is reset to the beginning of the traversal */ public void reset() { current = 0; prefixSum = 0; } /** * Determine if some of the elements have yet to be considered. * * @post returns true if there is more structure to be traversed * * @return True if more elements are to be considered. */ public boolean hasNext() { return current < theVector.size(); } /** * Fetch a reference to the current value. * * @pre traversal has more elements * @post returns the current value referenced by the iterator * * @return A reference to the current value being considered. */ public Integer get() { return new Integer(prefixSum+theVector.get(current).intValue()); } /** * Return current value, and increment iterator to next * value to be returned (the next even-numbered entry) * * @pre traversal has more elements * @post increments the iterated traversal * * @return the prefix sum of the values up to the current */ public Integer next() { prefixSum += theVector.get(current).intValue(); current++; return new Integer(prefixSum); } }
import structure5.*; /** * An iterator class for implementing an iterator over a Vector that * returns only the elements at even-numbered indices. * * Based on VectorIterator from Duane Bailey's stucture package * * @version $Id: VectorEvenIterator.java 950 2009-10-07 02:09:52Z terescoj $ * @author, Jim Teresco, 2009 */ public class VectorEvenIterator<E> extends AbstractIterator<E> { /** * The associated vector */ protected Vector<E> theVector; /** * The index of the current value. */ protected int current; /** * Construct a vector iterator to traverse vector v * * @post constructs an initialized iterator associated with v * * @param v The underlying vector. */ public VectorEvenIterator(Vector<E> v) { theVector = v; reset(); } /** * Reset the vector iterator to the first value in the vector. * * @post the iterator is reset to the beginning of the traversal */ public void reset() { current = 0; } /** * Determine if some of the elements have yet to be considered. * * @post returns true if there is more structure to be traversed * * @return True if more elements are to be considered. */ public boolean hasNext() { return current < theVector.size(); } /** * Fetch a reference to the current value. * * @pre traversal has more elements * @post returns the current value referenced by the iterator * * @return A reference to the current value being considered. */ public E get() { return theVector.get(current); } /** * Return current value, and increment iterator to next * value to be returned (the next even-numbered entry) * * @pre traversal has more elements * @post increments the iterated traversal by 2 * * @return A reference to the current value, before increment. */ public E next() { E result = theVector.get(current); current+=2; return result; } }