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.

So, to avoid the risks of modify<strong>in</strong>g a list that has created multiple iterators (by calls<br />

to its iterator() method), java.util.Iterator objects have a "fail-fast"<br />

feature that immediately <strong>in</strong>validates such an iterator if its underly<strong>in</strong>g collection is<br />

modified unexpectedly. For example, if a java.util.L<strong>in</strong>kedList object L<br />

has returned five different iterators <strong>and</strong> one of them modifies L, then the other four<br />

all become immediately <strong>in</strong>valid. That is, <strong>Java</strong> allows many list iterators to be<br />

travers<strong>in</strong>g a l<strong>in</strong>ked list L at the same time, but if one of them modifies L (us<strong>in</strong>g an<br />

add, set, or remove method), then all the other iterators for L become <strong>in</strong>valid.<br />

Likewise, if L is modified by one of its own update methods, then all exist<strong>in</strong>g<br />

iterators for L immediately become <strong>in</strong>valid.<br />

The java.util.List Interface <strong>and</strong> Its Implementations<br />

<strong>Java</strong> provides functionality similar to our array list <strong>and</strong> node lists ADT <strong>in</strong> the<br />

java.util.List <strong>in</strong>terface, which is implemented with an array <strong>in</strong><br />

java.util.ArrayList <strong>and</strong> with a l<strong>in</strong>ked list <strong>in</strong><br />

java.util.L<strong>in</strong>kedList. There are some trade-offs between these two<br />

implementations, which we explore <strong>in</strong> more detail <strong>in</strong> the next section. Moreover,<br />

<strong>Java</strong> uses iterators to achieve a functionality similar to what our node list ADT<br />

derives from positions. Table 6.4 shows correspond<strong>in</strong>g methods between our<br />

(array <strong>and</strong> node) list ADTs <strong>and</strong> the java.util <strong>in</strong>terfaces List <strong>and</strong><br />

ListIterator <strong>in</strong>terfaces, with notes about their implementations <strong>in</strong> the<br />

java.util classes ArrayList <strong>and</strong> L<strong>in</strong>kedList.<br />

Table 6.4: Correspondences between methods <strong>in</strong><br />

the array list <strong>and</strong> node list ADTs <strong>and</strong> the java.util<br />

<strong>in</strong>terfaces List <strong>and</strong> ListIterator. We use A <strong>and</strong> L<br />

as abbreviations for java.util.ArrayList <strong>and</strong><br />

java.util.L<strong>in</strong>ked List (or their runn<strong>in</strong>g times).<br />

List ADT Method<br />

java.util.List Method<br />

ListIterator Method<br />

Notes<br />

size()<br />

size()<br />

O(1) time<br />

348

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

Saved successfully!

Ooh no, something went wrong!