List Iterators

LinkedList list = . . .;
ListIterator iterator = list.listIterator();
while (iterator.hasNext())
{
Object current = iterator.next();
. . .
}

Classical List Data Structure

High-Level View of Data Structures

List with Cursor

.

for (list.reset(); list.hasNext(); list.next())
{
Object x = list.get();
. . .
}

The Pattern Concept

Short Passages Pattern

.

Short Passages Pattern

Context

"...Long, sterile corridors set the scene for everything bad about modern architecture..."

Problem

a lengthy description of the problem, including

Short Passages Pattern

Solution

Keep passages short. Make them as much like rooms as possible, with carpets or wood on the floor, furniture, bookshelves, beautiful windows. Make them generous in shape and always give them plenty of light; the best corridors and passages of all are those which have windows along an entire wall.

.

Iterator Pattern

Context

  1. An aggregate object contains element objects
  2. Clients need access to the element objects
  3. The aggregate object should not expose its internal structure
  4. Multiple clients may want independent access

Iterator Pattern

Solution

  1. Define an iterator that fetches one element at a time
  2. Each iterator object keeps track of the position of the next element
  3. If there are several aggregate/iterator variations, it is best if the aggregate and iterator classes realize common interface types.

Iterator Pattern

.

Iterator Pattern

Name in Design Pattern
Actual Name (linked lists)
Aggregate
List
ConcreteAggregate
LinkedList
Iterator
ListIterator
ConcreteIterator
anonymous class implementing ListIterator
createIterator()
listIterator()
next()
next()
isDone()
opposite of hasNext()
currentItem()
return value of hasNext()



Maintained by John Loomis, last updated 25 February 2007