using iterators trey chandler burlington williams high school mr. langner’s class, 2005-2006
TRANSCRIPT
![Page 1: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/1.jpg)
Using Iterators
Trey Chandler
Burlington Williams High School
Mr. Langner’s Class, 2005-2006
![Page 2: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/2.jpg)
List Traversal
• What is it?– A way of moving through linked data structures
node by node– It can be used with plenty of classes, such as
LinkedList, Set, etc.
![Page 3: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/3.jpg)
Methods of Traversal
• Using the getNext() method of the ListNode class
• creating a current instance variable that refers to a particular node in a linked structure
![Page 4: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/4.jpg)
But,
There are some problems!!!!!!!!!
• For Example, what happens if you want to access the list in two places simultaneously?
![Page 5: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/5.jpg)
The solution is...
The Iterator!
![Page 6: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/6.jpg)
So, What is an iterator?
• Iterator is an iterface in the java.util package.
• Iterators advance through the nodes of a linked data structure one by one
![Page 7: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/7.jpg)
Methods of Iterator
• boolean hasNext()– returns true if there are more elements to be
examined, false otherwise
• Object next()– returns the next element
• void remove()– removes the last element returned by next
![Page 8: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/8.jpg)
hasNext()
Current
• In this case hasNext() would return true
![Page 9: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/9.jpg)
hasNext()
Current
• In this case hasNext() would return false
![Page 10: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/10.jpg)
next()
• The first call to next() results in the first element of the the data structure.
• To begin with, the list looks like this
Current
![Page 11: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/11.jpg)
next()
• When next is called, it looks like this
Current
![Page 12: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/12.jpg)
next()
• When next is called again, it looks like this
Current
![Page 13: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/13.jpg)
next()
• When next is called again, it looks like this
Current
If next is called one more time, an exception is thrown
![Page 14: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/14.jpg)
remove()
• remove() can only be used once per call to next()
• next() must be called at least once before remove() is called
• If next() is not called before, an exception is thrown.
![Page 15: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/15.jpg)
remove()
• If a the current node is position one (meaning next() would result in pos. 2)
• and remove() is called...
Current
1 2 3
![Page 16: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/16.jpg)
remove()
• The linked list would look like this
Current
2 3
![Page 17: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/17.jpg)
ListIterator
• ListIterator is an extension of the Iterator interface
• It has two additional methods: void set(Object o) and add(Object o)
![Page 18: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/18.jpg)
set(Object o)
• Sets the current node to Object o.
• For example:
Current
x y z
• With this LinkedList, a call to set(w) would produce...
![Page 19: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/19.jpg)
This!
Current
x w z
![Page 20: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/20.jpg)
add(Object o)
• Adds a node immediately before what would be the result of next()
Current
x w z
• For example, a call of add(v) would produce...
![Page 21: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/21.jpg)
This!
Current
x w zv
![Page 22: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/22.jpg)
Creating an instance interator
• Oftentimes, an instance of Iterator of ListIterator can be created by using the iterator() or listIterator() method of a class.
• For LinkedList g, Iterator x = g.iterator() would produce an instance of Iterator called x that begins at g’s first node.
• On your AP Test, refer to the java subset to see which classes have these methods.
![Page 23: Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006](https://reader035.vdocuments.site/reader035/viewer/2022070411/56649f455503460f94c66021/html5/thumbnails/23.jpg)
Bibliography
• Barron’s How to Prepare for the AP Computer Science Advanced Placement Examination Java Version
• http://java.sun.com/j2se/1.4.2/docs/api/java/util/Iterator.html