20Iterator

# Well use the value null to denote the situation where

• Notes
• 40

This preview shows page 22 - 38 out of 40 pages.

We’ll use the value null to denote the situation where the iterator is positioned to the left of the list. The first time next() is called, we’ll move current to the first node and return its value. For each successive call, current is moved one position forward and the value found at that node is returned.

Subscribe to view the full document.

head l B C A current Iterator i = l.iterator(); head l B C A current after i.next(); head l B C A current after i.next(); head l B C A current after i.next();
Implementing iterator(). public class LinkedList<E> implements List<E>, Iterator<E> { private static class Node<E> { ... } private Node<E> head; private Node<E> tail; private Node<E> current; // <--- public Iterator<E> iterator() { current = null; return this; } }

Subscribe to view the full document.

But do we need iterator() ? Couldn’t we simply assign null in the constructor of the list? No, the method is necessary to traverse the list more than one time. int sum = 0; int n = 0; Iterator<Integer> i = l.iterator(); while ( i.hasNext() ) { Integer v = i.next(); sum += v.intValue(); n++; } i = l.iterator(); while ( i.hasNext() ) { Integer v = i.next(); if ( v.intValue() > ( sum / n ) ) { System.out.println( "is greater than average" ); } }
Implementing next . public class LinkedList<E> implements List<E>, Iterator<E> { private static class Node<E> { ... } private Node<E> head; private Node<E> current; public Iterator<E> iterator() { ... } public E next() { if ( current == null ) { current = head; } else { current = current.next; } if ( current == null ) { throw new NoSuchElementException(); } return current.value; } }

Subscribe to view the full document.

head l B C A current
head l B C A current

Subscribe to view the full document.

head l B C A current
head l B C A current

Subscribe to view the full document.

Current limitation Our current implementation does not allow for multiple iterators! while ( i.hasNext() ) { oi = i.next(); while ( j.hasNext() ) { oj = i.next(); // process oi and oj } }
What’s needed then? We need as many references (variables) as iterators; An iterator has to have access to the implementation (Node) ; An iterator needs access to the instance variables of the class LinkedList .

Subscribe to view the full document.

Allowing for multiple iterators Suggestions?
Implementation -1.5- Let’s create the following top-level class (in the same package as LinkedList ), this will require that i) Node is also a top-level class and ii) the variable head should be protected .

Subscribe to view the full document.

class LinkedListIterator<E> implements Iterator<E> { private Node<E> current; private LinkedList<E> myList; LinkedListIterator( LinkedList<E> myList ) { this.myList = myList; current = null; } public boolean hasNext() { return ( ( current == null && myList.head != null ) || ( current != null && current.next != null) ); } public E next() { if ( current == null ) { current = myList.head; } else { current = current.next; } return current.value; } }
head l D B C A current i current j myList myList

Subscribe to view the full document.

The method iterator() of the class LinkedList is defined as follows.
You've reached the end of this preview.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern