I 5 5 inext l first modcount 60 5 current modcount i

This preview shows page 52 - 65 out of 69 pages.

i 5 5 i.next(); l first last 3.0 2.0 modCount 6.0 5 current modCount i 5 i.next(); l first last 3.0 2.0 modCount 6.0 5 current modCount i 5 i.next(); Similarly, traversing the list does not change modCount .
Image of page 52

Subscribe to view the full document.

l first last 3.0 2.0 modCount 6.0 current modCount i current modCount j 5 5 5 Both iterators are valid l first last 3.0 2.0 modCount current modCount j 6 6 current modCount i 5 j.remove(); The iterator whose expectedModCount has the same value as the one in the header of the list remains valid; all the other ones are not.
Image of page 53
Changes to the class LinkedList The list operations that modify the list, addFist , deleteFirst , etc. must now change the value of modCount . When creating a new list, modCount must be set to zero.
Image of page 54

Subscribe to view the full document.

Modifications to ListIterator When creating a new iterator, the value modCount from the header of the list must be copied to the instance variable expectedModCount . The methods for adding and removing elements must update both, their own expectedModCount and modCount in the header of the list. All the iterator’s methods must be changed so that the precondition “is valid?” is true, otherwise they must throw the appropriate exception, ConcurrentModificationException .
Image of page 55
isValid() To facilitate writing the methods of the inner class ListIterator , we create a new method called isValid() . The method checks that expectedModCount == modCount .
Image of page 56

Subscribe to view the full document.

Iterator and new for loop List<Integer> ints = Arrays.asList( 1, 2, 3, 4, 5 ); int s = 0; for ( Iterator<Integer> it = ints.iterator(); it.hasNext(); ) { Integer i = it.next(); s += i.intValue(); } System.out.println( s );
Image of page 57
Iterator and new for loop Java 5 introduces a new syntax for the for loop that automagically creates and uses an iterator to traverse a list, and unboxes the elements returned by the iteration. List<Integer> ints = Arrays.asList( 1, 2, 3, 4, 5 ); int s = 0; for ( int i : ints ) { s += i; } System.out.println( s );
Image of page 58

Subscribe to view the full document.

Iterator and new for loop List<Integer> ints = Arrays.asList( 1, 2, 3, 4, 5 ); int s = 0; for ( int i : ints ) { s += i; } System.out.println( s ); What type can be used with the new for loop? Any type that has an iterator. What is the Java way to enforce this? Any class that implements the interface Iterable < E > .
Image of page 59
Iterable < E > public interface Iterable<E> { public Iterator<E> iterator(); } where public interface Iterator<E> { public boolean hasNext(); public E next(); public void remove(); }
Image of page 60

Subscribe to view the full document.

Iterable public class LinkedList<E> implements List<E>, Iterable<E> { private static class Node<T> { ... } private Node<E> head; private class ListIterator implements Iterator<E> { ... } ... }
Image of page 61
Iterable public interface List<E> extends Iterable<E> { ... } public class LinkedList<E> implements List<E> { private static class Node<T> { ... } private Node<E> head; private class ListIterator implements Iterator<E> { ... } ... }
Image of page 62

Subscribe to view the full document.

Arrays and new for loop For the sake of completeness, the new for loop can also be applied to arrays. int[] xs = new int[] { 1, 2, 3 }; int sum = 0; for ( int x : xs ) { sum += x; }
Image of page 63
Appendix : Accessing Members of the Outer Object In the previous example, the modification counter of the inner class ( expectedModCount ) was given a different name than that of the outer class ( modCount ) to avoid a name conflict.
Image of page 64

Subscribe to view the full document.

Image of page 65
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern