Topic 14pIterators"First things first, but not necessarily itht d "in that order "-Dr Who-Dr. WhoCS 307 Fundamentals of Computer Science Iterators1A Questionbli l W dLi t {public class WordList {private ArrayList<String> myList; // pre: none// post: all words that are exactly len// characters long have been removed from// characters long have been removed from// this WordList with the order of the// remaining words unchangedpublic void removeWordsOfLength(int len){public void removeWordsOfLength(int len){for(int i = 0; i < myList.size(); i++){if( myList.get(i).length() == len )Li t (i)myList.remove(i);} CS 307 Fundamentals of Computer Science Iterators2Attendance Question 1When does method removeWordsOfLength work as intended?A. AlwaysBSometimesB.SometimesC. Never// original list = [“dog”, “cat”, “hat”, “sat”]// resulting list afterremoveWordsOfLength(3) ?// resulting list after removeWordsOfLength(3) ?CS 307 Fundamentals of Computer Science Iterators3The Remove QuestionA?Answer?public void removeWordsOfLength(int len) {Iterator<String> it = myList iterator();Iterator<String> it = myList.iterator();while( it.hasNext() )if( it.next().length() == len )it.remove();}}// original list = [“dog”, “cat”, “hat”, “sat”]// resulting list afterremoveWordsOfLength(3) ?CS 307 Fundamentals of Computer Science Iterators4// resulting list after removeWordsOfLength(3) ?IteratorsArrayList 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 orderSo eCo ec o sdo a eade eode– Sets, Maps, GraphsHow to access all the items in a CollectionHow to access all the items in a Collection with no specified order?CS 307 Fundamentals of Computer Science Iterators5Access All Elements - ArrayListiii iipublic 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 Iterators6Iterator InterfaceA it t bj t i “ h t” bj tAn iterator object is a “one shot” object– it is designed to go through all the elements of a Collection onceelements of a Collection once– if you want to go through the elements of a Collection again youelements of a Collection again you have to get another iterator objectIterators are obtained by calling yga method from the CollectionCS 307 Fundamentals of Computer Science Iterators7Iterator MethodsThe Iterator interface specifies 3 methods:The Iterator interface specifies 3 methods:boolean hasNext()//returns true if this iteration has more elementsObject next()//returns the next element in this iteration//returns the next element in this iteration//pre: hastNext()id ()void remove()/*Removes from the underlying collection the last element returned by the iterator.Thi th d b ll d l ll t tpre: This method can be called only once per call to next. After calling, must call next again before calling remove again. */CS 307 Fundamentals of Computer Science Iterators8*/Attendance Question 2Which of the following produces a syntax error?ArrayList list = new ArrayList(); Iterator it1 = new Iterator(); // IIteratorit2 newIterator(list); // IIIteratorit2 = new Iterator(list); // II Iterator it3 = list.iterator(); // IIIAIA. IB. IICIIIC. IIID. I and IIE. II and IIICS 307 Fundamentals of Computer Science Iterators9Typical Iterator Patterniii iipublic void printAll(ArrayList list){Iterator it = list.iterator();Obj t tObject temp;while( it.hasNext() ) {itemp = it.next();System.out.println( temp );}}CS 307 Fundamentals of Computer Science Iterators10Typical Iterator Pattern 2public void printAll(ArrayList list){public void printAll(ArrayList list){Iterator it = list.iterator();while( it.hasNext() )S t t i tl ( it t() )System.out.println( it.next() );}// i// 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 Iterators11A Picture of an IteratorImagine a fence made up of fence posts and rail sectionsrailsrailsfencepostsCS 307 Fundamentals of Computer Science Iterators12Fence 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 theAs long as a rail exists to the right of the Iterator, hasNext() is trueiterator objectjCS 307 Fundamentals of Computer Science Iterators13Fence AnalogyALitStiArrayList<String> names = new ArrayList<String>();names.add(“Jan”);names.add(“Levi”);names.add(“Tom”);names add(“Jose”);names.add( Jose );Iterator<String> it = names.iterator();int i = 0;“Jan” “Levi” “Tom” “Jose”CS 307 Fundamentals of Computer Science Iterators14Fence Analogyiiwhile( it.hasNext() ) {i++;System.out.println( it.next() );}// when i == 1, prints out Janfirst call to next moves iterator tonext post and returns “Jan”“Jan” “Levi” “Tom” “Jose”next post and returns “Jan”CS 307 Fundamentals of Computer Science Iterators15Fence Analogyiiwhile( it.hasNext() ) {i++;System.out.println( it.next() );}// when i == 2, prints out Levi“Jan” “Levi” “Tom” “Jose”CS 307 Fundamentals of Computer Science Iterators16Fence Analogyiiwhile( it.hasNext() ) {i++;System.out.println( it.next() );}// when i == 3, prints out Tom“Jan” “Levi” “Tom” “Jose”CS 307 Fundamentals of Computer Science Iterators17Fence Analogyiiwhile( it.hasNext() ) {i++;System.out.println( it.next() );}// when i == 4, prints out Jose“Jan” “Levi” “Tom” “Jose”CS 307 Fundamentals of Computer Science Iterators18Fence Analogyiiwhile( it.hasNext() ) {i++;System.out.println( it.next() );}// call to hasNext returns false// while loop stops“Jan” “Levi” “Tom” “Jose”CS 307 Fundamentals of Computer Science Iterators19Attendance Question 3What is output by the following code?ArrayList<Integer> list;List = new ArrayList<Integer>();list.add(3);list
View Full Document