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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

then call iterator() on this object to get an iterator of positions from the<br />

orig<strong>in</strong>al list.<br />

Code Fragment 6.16: Add<strong>in</strong>g iterator methods to<br />

the Position List <strong>in</strong>terface.<br />

Code Fragment 6.17: The positions() method of<br />

class NodePositionList.<br />

The iterator() method returned by this <strong>and</strong> other Iterable objects def<strong>in</strong>es<br />

a restricted type of iterator that allows only one pass through the elements. More<br />

powerful iterators can also be def<strong>in</strong>ed, however, which allows us to move forward<br />

<strong>and</strong> backward over a certa<strong>in</strong> order<strong>in</strong>g of the elements.<br />

6.3.4 List Iterators <strong>in</strong> <strong>Java</strong><br />

The java.util.L<strong>in</strong>ked List class does not expose a position concept to<br />

users <strong>in</strong> its API. Instead, the preferred way to access <strong>and</strong> update a L<strong>in</strong>kedList<br />

object <strong>in</strong> <strong>Java</strong>, without us<strong>in</strong>g <strong>in</strong>dices, is to use a ListIterator that is generated<br />

by the l<strong>in</strong>ked list, us<strong>in</strong>g a listIterator() method. Such an iterator provides<br />

forward <strong>and</strong> backward traversal methods as well as local update methods. It views<br />

its current position as be<strong>in</strong>g before the first element, between two elements, or after<br />

346

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

Saved successfully!

Ooh no, something went wrong!