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.

java.lang.Iterable, which has only this method <strong>in</strong> it. This method can<br />

make it simple for us to specify computations that need to loop through the<br />

elements of a list. To guarantee that a node list supports the above methods, for<br />

example, we could add this method to the Position List <strong>in</strong>terface, as shown<br />

<strong>in</strong> Code Fragment 6.12. In this case, we would also want to state that Position<br />

List extends Iterable. Therefore, let us assume that our array lists <strong>and</strong><br />

node lists lists support the iterator() method.<br />

Code Fragment 6.12: Add<strong>in</strong>g the iterator method to<br />

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

Given such a Position List def<strong>in</strong>ition, we could use an iterator<br />

returned by the iterator() method to create a str<strong>in</strong>g representation of a node<br />

list, as shown <strong>in</strong> Code Fragment 6.13.<br />

Code Fragment 6.13: Example of a <strong>Java</strong> iterator<br />

used to convert a node list to a str<strong>in</strong>g.<br />

6.3.2 The <strong>Java</strong> For-Each Loop<br />

342

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

Saved successfully!

Ooh no, something went wrong!