Iterators
-
Upload
susannah-short -
Category
Documents
-
view
18 -
download
0
description
Transcript of Iterators
![Page 1: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/1.jpg)
Iterators
CS 367 – Introduction to Data Structures
![Page 2: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/2.jpg)
Iterator
• Allows a user to step through each item in a data structure– array, vector, linked list, tree, etc.
• Two basic components– hasNext()
• returns true if the iterator has any more items
– next()• returns the next item in the iterator
![Page 3: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/3.jpg)
Example
• Print out each item in a vector
Vector v = new Vector();
addItems(v);
Iterator it = v.iterator();
while(it.hasNext())
System.out.println(it.next().toString());
![Page 4: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/4.jpg)
Iterator
hasNext() == true
next
hasNext() == true
next
hasNext() == false
next
![Page 5: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/5.jpg)
Warning
• Note that the next pointer is out-of-bounds after the last call to next()– another call to next() will result in exception
• Should always call hasNext() before calling next()
![Page 6: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/6.jpg)
Implementation
• Three main ways to implement an iterator– single class for an iterator
• requires all data structures to implement an iterator specific interface
– create a separate, custom iterator class for each data structure
– create a custom iterator class for each data structure and make it an inner class inside the appropriate data structure
![Page 7: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/7.jpg)
Single Iterator Class
• Implement a single class called Iterator
• Force each data structure that allows iterators to implement an iterator interface
interface DSIterator {
public int size();
public Object getIndex(int index);
![Page 8: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/8.jpg)
Iterator Classclass Iterator {
private DSIterator ds;private int nextItem;public Iterator(DSIterator ds) {
this.ds = ds;nextItem = 0;
}public boolean hasNext() {
return nextItem < ds.size();}public Object next() {
Object obj = ds.getIndex(nextItem);nextItem++;return obj;
}}
![Page 9: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/9.jpg)
Questions
• What happens if user calls next() at end of data?
• How many times can user to through iterator?
• How does user go through the data again?
![Page 10: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/10.jpg)
Notes on Single Iterator Class
• Advantages– only have to write the iterator class once
• Disadvantage– performance can suffer greatly for some data
structures
![Page 11: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/11.jpg)
Linked List Example
class LinkedList implements DSIterator {// start of normal linked list code. . .// end of normal linked list codeprivate int size; // increment (decrement) in add (delete) methodspublic int size() {
return size;}public Object getIndex(int index) {
Object tmp = head;for(int i=0; i<index; i++, tmp = tmp.next);return tmp;
}
![Page 12: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/12.jpg)
Linked List Example
• Notice the problem with the getIndex() method– O(n) performance
• Want our iterator to return the next item in O(1) time
• Will need a different implementation of the iterator for a list than for a vector
![Page 13: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/13.jpg)
Custom Class
• Previous example shows the need for custom iterator classes for different data structures
• Custom class will:– take data structure object through constructor– set a pointer to the first data item– define next() method that works in O(1) time
![Page 14: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/14.jpg)
Linked List Implementation
class LLIterator {private Node nextItem;public LLIterator(LinkedList list) {
nextItem = list.head;}public boolean hasNext() {
return nextItem != null;}public Object next() {
Object obj = nextItem.data;nextItem = nextItem.next;return obj;
}}
![Page 15: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/15.jpg)
Notes on Custom Iterator Classes
• Advantage– very good performance
• next() is always O(1)
• Disadvantage– head of Linked List must be made public
• this is a bad thing – why?
– have to write a custom iterator for each data type
• means the user must know which one to use for which data type
![Page 16: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/16.jpg)
Nested Iterator Class
• Java allows one class to be nested inside another
• Nest class has full access to private data of main class– can now let iterator access head– head is still kept as a private field
![Page 17: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/17.jpg)
Full Example
• Another version of the linked list
interface Iterator {
public boolean hasNext();
public Object next();
}
![Page 18: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/18.jpg)
class LinkedList {
private Node head;
// put all the usual stuff here
public class LLIterator implements Iterator {
private Node nextObject;
public LLIterator() {
nextObject = head;
}
}
public Iterator iterator() {
return new Iterator();
}
}
![Page 19: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/19.jpg)
Notes on Nested Class
• Advantage– get the same performance as custom class
• it still is a custom class
– encapsulation remains in tact• head gets to stay private
• Disadvantage– custom iterator for each data structure– a bit more confusing to learn and understand
![Page 20: Iterators](https://reader036.fdocuments.us/reader036/viewer/2022083005/56813578550346895d9cdcef/html5/thumbnails/20.jpg)
Using Iterators• Assume a nested iterator class
public void printContents(Object ds) {Iterator it = ds.iterator();while(it.hasNext())
System.out.println(it.next().toString());}
• The above code will work with any data structure– of course, it assumes the data structure class
supports iterators