23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

An iterator is a software design pattern that abstracts the process of scann<strong>in</strong>g<br />

through a collection of elements one element at a time. An iterator consists of a<br />

sequence S, a current element <strong>in</strong> S, <strong>and</strong> a way of stepp<strong>in</strong>g to the next element <strong>in</strong> S<br />

<strong>and</strong> mak<strong>in</strong>g it the current element. Thus, an iterator extends the concept of the<br />

position ADT we <strong>in</strong>troduced <strong>in</strong> Section 6.2. In fact, a position can be thought of as<br />

an iterator that doesn't go anywhere. An iterator encapsulates the concepts of<br />

"place" <strong>and</strong> "next" <strong>in</strong> a collection of objects.<br />

We def<strong>in</strong>e the iterator ADT as support<strong>in</strong>g the follow<strong>in</strong>g two methods:<br />

hasNext(): Test whether there are elements left <strong>in</strong> the iterator.<br />

next(): Return the next element <strong>in</strong> the iterator.<br />

Note that the iterator ADT has the notion of the "current" element <strong>in</strong> a traversal of a<br />

sequence. The first element <strong>in</strong> an iterator is returned by the first call to the method<br />

next, assum<strong>in</strong>g of course that the iterator conta<strong>in</strong>s at least one element.<br />

An iterator provides a unified scheme to access all the elements of a collection of<br />

objects <strong>in</strong> a way that is <strong>in</strong>dependent from the specific organization of the collection.<br />

An iterator for an array list, list, or sequence should return the elements accord<strong>in</strong>g<br />

to their l<strong>in</strong>ear order<strong>in</strong>g.<br />

Simple Iterators <strong>in</strong> <strong>Java</strong><br />

<strong>Java</strong> provides an iterator through its java.util.Iterator <strong>in</strong>terface. We<br />

note that the java.util.Scanner class (Section 1.6) implements this<br />

<strong>in</strong>terface. This <strong>in</strong>terface supports an additional (optional) method to remove the<br />

previously returned element from the collection. This functionality (remov<strong>in</strong>g<br />

elements through an iterator) is somewhat controversial from an object-oriented<br />

viewpo<strong>in</strong>t, however, <strong>and</strong> it is not surpris<strong>in</strong>g that its implementation by classes is<br />

optional. Incidentally, <strong>Java</strong> also provides the java.util.Enumeration<br />

<strong>in</strong>terface, which is historically older than the iterator <strong>in</strong>terface <strong>and</strong> uses names<br />

hasMoreElements() <strong>and</strong> nextElement().<br />

The Iterable Abstract <strong>Data</strong> Type<br />

In order to provide a unified generic mechanism for scann<strong>in</strong>g through a data<br />

structure, ADTs stor<strong>in</strong>g collections of objects should support the follow<strong>in</strong>g<br />

method:<br />

iterator(): Return an iterator of the elements <strong>in</strong> the collection.<br />

This method is supported by the java.util.ArrayList class. In fact, this<br />

method is so important, that there is a whole <strong>in</strong>terface,<br />

341

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!