Iterators First things first but not necessarily in
Iterators "First things first, but not necessarily in that order " -Dr. Who
Iterator Interface 8 An iterator object is a “one shot” object – it is designed to go through all the elements of an ADT once – if you want to go through the elements of an ADT again, you have to get another iterator object 8 Iterators are obtained by calling the iterator method CS 221 - Computer Science II 2
Iterator Interface Methods 8 The Iterator interface lists 3 methods: boolean has. Next() //returns true if this iteration has more elements T next() //returns the next element in this iteration //pre: has. Next returns true void remove() /* Optional method. Removes the last element returned by the Iterator. pre: Can only be called only once for each call to next. After calling, must call next again before calling remove again. */ CS 221 - Computer Science II 3
Question 1 8 Which of the following will not compile? Array. List<String> list Iterator<String> it 1 = Iterator<String> it 2 = Iterator<String> it 3 = = new Array. List<String>(); new Iterator(); // I new Iterator(list); // II list. iterator(); // III A. I B. II C. III D. I and II E. II and III CS 221 - Computer Science II 4
Question 1 8 Which of the following will not compile? Array. List<String> list Iterator<String> it 1 = Iterator<String> it 2 = Iterator<String> it 3 = = new Array. List<String>(); new Iterator(); // I new Iterator(list); // II list. iterator(); // III A. I B. II C. III D. I and II E. II and III CS 221 - Computer Science II 5
Visualizing an Iterator 8 Imagine a fence made up of fence posts and rail sections rails fenceposts CS 221 - Computer Science II 6
Fence Analogy 8 The iterator lives on the fence posts 8 The data in the collection are the rails 8 Iterator created at the far left post 8 As long as a rail exists to the right of the iterator, has. Next() is true iterator object CS 221 - Computer Science II 7
Fence Analogy Array. List<String> names = new Array. List<String>(); names. add(“Jan”); names. add(“Levi”); names. add(“Tom”); names. add(“Jose”); Iterator<String> it = names. iterator(); int i = 0; “Jan” “Levi” “Tom” “Jose” CS 221 - Computer Science II 8
Fence Analogy while( it. has. Next() ) { i++; System. out. println( it. next() ); } // when i == 1, prints out Jan first call to next() moves iterator to next post and returns “Jan” “Levi” “Tom” “Jose” CS 221 - Computer Science II 9
Fence Analogy while( it. has. Next() ) { i++; System. out. println( it. next() ); } // when i == 2, prints out Levi “Jan” “Levi” “Tom” “Jose” CS 221 - Computer Science II 10
Fence Analogy while( it. has. Next() ) { i++; System. out. println( it. next() ); } // when i == 3, prints out Tom “Jan” “Levi” “Tom” “Jose” CS 221 - Computer Science II 11
Fence Analogy while( it. has. Next() ) { i++; System. out. println( it. next() ); } // when i == 4, prints out Jose “Jan” “Levi” “Tom” “Jose” CS 221 - Computer Science II 12
Fence Analogy while( it. has. Next() ) { i++; System. out. println( it. next() ); } // call to has. Next returns false // while loop stops “Jan” “Levi” “Tom” “Jose” CS 221 - Computer Science II 13
Typical Iterator Pattern Iterator<T> it = list. iterator(); while( it. has. Next() ) { T temp = it. next(); // do something with temp } CS 221 - Computer Science II 14
Question 2 8 What is output of the following code? Array. List<Integer> list; list = new Array. List<Integer>(); list. add(3); list. add(5); Iterator<Integer> it = list. iterator(); System. out. println(it. next()); A. 3 D. 3 3 B. 5 E. 3 5 CS 221 - Computer Science II C. 3 3 5 15
Question 2 8 What is output of the following code? Array. List<Integer> list; list = new Array. List<Integer>(); list. add(3); list. add(5); Iterator<Integer> it = list. iterator(); System. out. println(it. next()); A. 3 D. 3 3 OR B. 5 E. 3 5 OR CS 221 - Computer Science II C. 3 3 5 F. 5 3 16
remove method 8 An Iterator an be used to remove things from an ADT 8 Can only be called once for each call to next public void remove. Words(Iterable<String> my. List, int len) { Iterator<String> it = my. List. iterator while( it. has. Next() ) { String temp = it. next(); if(temp. length() == len) it. remove(); } } Given list = [“dog”, “fish”, “cat”, “gerbil”], what is resulting list after call to remove. Words(3)? CS 221 - Computer Science II 17
Question 3 public void print. Target(Array. List<String> names, int len) { Iterator<String> it = names. iterator(); while( it. has. Next() ) if( it. next(). length() == len ) System. out. println( it. next() ); } Given names = [“Jan”, “Ivan”, “Tom”, “George”] and len = 3, what is output by the print. Target method? A. B. C. D. E. Jan Ivan Tom George Jan Tom Ivan George No output due to syntax error No output due to runtime error CS 221 - Computer Science II 18
Question 3 public void print. Target(Array. List<String> names, int len) { Iterator<String> it = names. iterator(); while( it. has. Next() ) if( it. next(). length() == len ) System. out. println( it. next() ); } Given names = [“Jan”, “Ivan”, “Tom”, “George”] and len = 3, what is output by the print. Target method? A. B. C. D. E. Jan Ivan Tom George Jan Tom Ivan George No output due to syntax error No output due to runtime error CS 221 - Computer Science II 19
The Iterable Interface 8 An interface implemented by an ADT 8 One method in the interface: public Iterator<T> iterator() 8 Can use typical iterator pattern to access all elements in ADT: public void do. Something(Iterable<T> list) Iterator<T> it = list. iterator(); while( it. has. Next() ) { T temp = it. next(); // do something with temp } } CS 221 - Computer Science II 20
Using Iterable ADTs 8 But if you simply want to go through all the elements of an Iterable ADT, use for-each loop – hides creation of the Iterator public void do. Something(Iterable<T> list) { for(T temp : list) { // do something with temp } } CS 221 - Computer Science II 21
Using Iterable ADTs 8 Can also use typical for loop public void do. Something(Iterable<T> list) { for (Iterator<T> itr = list. iterator(); itr. has. Next(); ) { T temp = iter. next(); // do something with temp } } CS 221 - Computer Science II 22
Implementing an Iterator 8 Nested / Inner Class – Example of encapsulation 8 Key Implementation Details – does your ADT need an Iterator? – keeping track of “location” in ADT – checking whether ADT has been modified since Iterator has been instantiated – checking precondition on remove method CS 221 - Computer Science II 23
Comodification 8 If a ADT with an Iterator is changed after it’s been instantiated, a Concurrent. Modification. Exception will be thrown the next time call next or remove methods are called Array. List<String> names = new Array. List<String>(); names. add(“Jan”); Iterator<String> it = names. iterator(); names. add(“Andy”); it. next(); // exception will occur here CS 221 - Computer Science II 24
- Slides: 24