Introduction to OOP: Chapter 8: The Solitaire Game [next] [previous] [audio] [real] [text]

Loop Iterators

class ListIterator {
	public ListIterator (Link firstLink)
		{ currentLink = firstLink; }

	public boolean atEnd () { return currentLink == null; }

	public void next ()
		{ if (currentLink != null) currentLink = currentLink.next();}

	public Object current ()
		{ if (currentLink == null)
			return null;
		  return currentLink.value(); }

	private Link currentLink;
}
Intro OOP, Chapter 8, Slide 11

This program was written using an early version of Java. Later versions of Java provided a class Enumeration to support the iterator concept. Unfortunately, the Enumeration protocol is slightly different from the protocol described here, although the purpose is the same.