Topic 14 IteratorsA QuestionAttendance Question 1The Remove QuestionIteratorsAccess All Elements - ArrayListIterator InterfaceIterator MethodsAttendance Question 2Typical Iterator PatternTypical Iterator Pattern 2A Picture of an IteratorFence AnalogySlide 14Slide 15Slide 16Slide 17Slide 18Slide 19Attendance Question 3Comodificationremove methodCommon Iterator ErrorThe Iterable InterfaceIterableImplementing an IteratorCS 307 Fundamentals of Computer Science Iterators1Topic 14Iterators"First things first, but not necessarily in that order "-Dr. WhoCS 307 Fundamentals of Computer Science Iterators2A Questionpublic class WordList {private ArrayList<String> myList; // pre: none// post: all words that are exactly len// characters long have been removed from// this WordList with the order of the// remaining words unchangedpublic void removeWordsOfLength(int len){for(int i = 0; i < myList.size(); i++){if( myList.get(i).length() == len )myList.remove(i);}Attendance Question 1When does method removeWordsOfLength work as intended?A.AlwaysB.SometimesC.Never// original list = [“dog”, “cat”, “hat”, “sat”]// resulting list after removeWordsOfLength(3) ?CS 307 Fundamentals of Computer Science Iterators3CS 307 Fundamentals of Computer Science Iterators4The Remove QuestionAnswer?public void removeWordsOfLength(int len) {Iterator<String> it = myList.iterator();while( it.hasNext() )if( it.next().length() == len )it.remove();}}// original list = [“dog”, “cat”, “hat”, “sat”]// resulting list after removeWordsOfLength(3) ?CS 307 Fundamentals of Computer Science Iterators5IteratorsArrayList is part of the Java Collections frameworkCollection is an interface that specifies the basic operations every collection (data structure) should haveSome Collections don’t have a definite order–Sets, Maps, GraphsHow to access all the items in a Collection with no specified order?CS 307 Fundamentals of Computer Science Iterators6Access All Elements - ArrayListpublic void printAll(ArrayList list){ for(int i = 0; i < list.size(); i++) System.out.println(list.get(i));}How do I access all the elements of a Set? The elements don’t have an index.Iterator objects provide a way to go through all the elements of a Collection, one at a timeCS 307 Fundamentals of Computer Science Iterators7Iterator InterfaceAn iterator object is a “one shot” object–it is designed to go through all the elements of a Collection once–if you want to go through the elements of a Collection again you have to get another iterator objectIterators are obtained by calling a method from the CollectionCS 307 Fundamentals of Computer Science Iterators8Iterator MethodsThe Iterator interface specifies 3 methods:boolean hasNext()//returns true if this iteration has more elementsObject next()//returns the next element in this iteration//pre: hastNext()void remove()/*Removes from the underlying collection the last element returned by the iterator. pre: This method can be called only once per call to next. After calling, must call next again before calling remove again. */Attendance Question 2Which of the following produces a syntax error?ArrayList list = new ArrayList(); Iterator it1 = new Iterator(); // IIterator it2 = new Iterator(list); // II Iterator it3 = list.iterator(); // IIIA. IB. IIC. IIID. I and IIE. II and IIICS 307 Fundamentals of Computer Science Iterators9CS 307 Fundamentals of Computer Science Iterators10Typical Iterator Patternpublic void printAll(ArrayList list){ Iterator it = list.iterator();Object temp;while( it.hasNext() ) {temp = it.next();System.out.println( temp );}}CS 307 Fundamentals of Computer Science Iterators11Typical Iterator Pattern 2public void printAll(ArrayList list){ Iterator it = list.iterator(); while( it.hasNext() ) System.out.println( it.next() );}// go through twice?public void printAllTwice(ArrayList list){ Iterator it = list.iterator(); while( it.hasNext() ) System.out.println( it.next() ); it = list.iterator(); while( it.hasNext() ) System.out.println( it.next() );}CS 307 Fundamentals of Computer Science Iterators12A Picture of an IteratorImagine a fence made up of fence posts and rail sectionsfencepostsrailsCS 307 Fundamentals of Computer Science Iterators13Fence AnalogyThe iterator lives on the fence postsThe data in the collection are the railsIterator created at the far left postAs long as a rail exists to the right of the Iterator, hasNext() is trueiterator objectCS 307 Fundamentals of Computer Science Iterators14Fence AnalogyArrayList<String> names = new ArrayList<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 307 Fundamentals of Computer Science Iterators15Fence Analogywhile( it.hasNext() ) { i++; System.out.println( it.next() );}// when i == 1, prints out Jan“Jan” “Levi” “Tom” “Jose”first call to next moves iterator tonext post and returns “Jan”CS 307 Fundamentals of Computer Science Iterators16Fence Analogywhile( it.hasNext() ) { i++; System.out.println( it.next() );}// when i == 2, prints out Levi“Jan” “Levi” “Tom” “Jose”CS 307 Fundamentals of Computer Science Iterators17Fence Analogywhile( it.hasNext() ) { i++; System.out.println( it.next() );}// when i == 3, prints out Tom“Jan” “Levi” “Tom” “Jose”CS 307 Fundamentals of Computer Science Iterators18Fence Analogywhile( it.hasNext() ) { i++; System.out.println( it.next() );}// when i == 4, prints out Jose“Jan” “Levi” “Tom” “Jose”CS 307 Fundamentals of Computer Science Iterators19Fence Analogywhile( it.hasNext() ) { i++; System.out.println( it.next() );}// call to hasNext returns false// while loop stops“Jan” “Levi” “Tom” “Jose”Attendance Question 3What is output by the following code?ArrayList<Integer> list;List = new ArrayList<Integer>();list.add(3);list.add(3);list.add(5);Iterator<Integer> it = list.iterator();System.out.println(it.next());System.out.println(it.next());A.3 B. 5 C. 3 3 5D. 3 3 E. 3 5CS 307 Fundamentals of Computer Science Iterators20CS 307 Fundamentals of Computer Science Iterators21ComodificationIf a
View Full Document