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.

Code Fragment 6.15: The iterator() method of<br />

class NodePositionList.<br />

Position Iterators<br />

For ADTs that support the notion of position, such as the list <strong>and</strong> sequence ADTs,<br />

we can also provide the follow<strong>in</strong>g method:<br />

positions(): Return an Iterable object (like an array list or node<br />

list) conta<strong>in</strong><strong>in</strong>g the positions <strong>in</strong> the collection as elements.<br />

An iterator returned by this method allows us to loop through the positions of a<br />

list. To guarantee that a node list supports this method, we could add it to the<br />

PositionList <strong>in</strong>terface, as shown <strong>in</strong> Code Fragment 6.16. Then we could, for<br />

example, add an implementation of this method to the NodePositionList, as<br />

shown <strong>in</strong> Code Fragment 6.17. This method uses the NodePositionList<br />

class itself to create a list that conta<strong>in</strong>s the positions of the orig<strong>in</strong>al list as its<br />

elements. Return<strong>in</strong>g this list of positions as our Iterable object allows us to<br />

345

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

Saved successfully!

Ooh no, something went wrong!