a question topic 14 p
play

A Question Topic 14 p public class WordList { bli l W dLi t { - PowerPoint PPT Presentation

A Question Topic 14 p public class WordList { bli l W dLi t { Iterators private ArrayList<String> myList; // pre: none "First things first, but not necessarily // post: all words that are exactly len // characters long have


  1. A Question Topic 14 p public class WordList { bli l W dLi t { Iterators private ArrayList<String> myList; // pre: none "First things first, but not necessarily // post: all words that are exactly len // characters long have been removed from // characters long have been removed from i in that order " th t d " // this WordList with the order of the // remaining words unchanged -Dr. Who -Dr Who public void removeWordsOfLength(int len){ public void removeWordsOfLength(int len){ for(int i = 0; i < myList.size(); i++){ if( myList.get(i).length() == len ) myList.remove(i); Li t (i) } CS 307 Fundamentals of CS 307 Fundamentals of 1 2 Computer Science Iterators Computer Science Iterators Attendance Question 1 The Remove Question � Answer? � A ? public void removeWordsOfLength(int len) { � When does method Iterator<String> it = myList.iterator(); Iterator<String> it = myList iterator(); removeWordsOfLength work as while( it.hasNext() ) intended? if( it.next().length() == len ) it.remove(); A. Always B. Sometimes B Sometimes C. Never } } // original list = [“dog”, “cat”, “hat”, “sat”] // original list = [“dog”, “cat”, “hat”, “sat”] // resulting list after removeWordsOfLength(3) ? // resulting list after removeWordsOfLength(3) ? // resulting list after removeWordsOfLength(3) ? // resulting list after removeWordsOfLength(3) ? CS 307 Fundamentals of 3 CS 307 Fundamentals of 4 Computer Science Iterators Computer Science Iterators

  2. Iterators Access All Elements - ArrayList � ArrayList is part of the Java Collections public void printAll(ArrayList list){ i i i i i framework for(int i = 0; i < list.size(); i++) System.out.println(list.get(i)); � Collection is an interface that specifies the } basic operations every collection (data structure) should have � How do I access all the elements of a Set? The � Some Collections don’t have a definite order So e Co ec o s do a e a de e o de elements don’t have an index. – Sets, Maps, Graphs � Iterator objects provide a way to go through all the � How to access all the items in a Collection How to access all the items in a Collection elements of a Collection, one at a time with no specified order? CS 307 Fundamentals of CS 307 Fundamentals of 5 6 Computer Science Iterators Computer Science Iterators Iterator Interface Iterator Methods � The Iterator interface specifies 3 methods: � The Iterator interface specifies 3 methods: � An iterator object is a “one shot” object � A it t bj t i “ h t” bj t boolean hasNext() – it is designed to go through all the //returns true if this iteration has more elements elements of a Collection once elements of a Collection once – if you want to go through the Object next() elements of a Collection again you elements of a Collection again you //returns the next element in this iteration //returns the next element in this iteration have to get another iterator object //pre: hastNext() � Iterators are obtained by calling y g void remove() id () a method from the Collection /*Removes from the underlying collection the last element returned by the iterator. pre: This method can be called only once per call to next. Thi th d b ll d l ll t t After calling, must call next again before calling remove again. */ */ CS 307 Fundamentals of 7 CS 307 Fundamentals of 8 Computer Science Iterators Computer Science Iterators

  3. Attendance Question 2 Typical Iterator Pattern � Which of the following produces a syntax public void printAll(ArrayList list){ i i i i i error? Iterator it = list.iterator(); Object temp; Obj t t ArrayList list = new ArrayList(); Iterator it1 = new Iterator(); // I while( it.hasNext() ) { Iterator it2 Iterator it2 = new Iterator(list); // II new Iterator(list); // II temp = it.next(); i Iterator it3 = list.iterator(); // III System.out.println( temp ); A. I A I } B. II } C III C. III D. I and II E. II and III CS 307 Fundamentals of CS 307 Fundamentals of 9 10 Computer Science Iterators Computer Science Iterators Typical Iterator Pattern 2 A Picture of an Iterator public void printAll(ArrayList list){ public void printAll(ArrayList list){ � Imagine a fence made up of fence posts and Iterator it = list.iterator(); rail sections while( it.hasNext() ) rails rails S System.out.println( it.next() ); t t i tl ( it t() ) } // // go through twice? i public void printAllTwice(ArrayList list){ Iterator it = list.iterator(); while( it.hasNext() ) System.out.println( it.next() ); it = list.iterator(); while( it.hasNext() ) fenceposts System.out.println( it.next() ); } CS 307 Fundamentals of 11 CS 307 Fundamentals of 12 Computer Science Iterators Computer Science Iterators

  4. Fence Analogy Fence Analogy ArrayList<String> names = A Li t St i � The iterator lives on the fence posts new ArrayList<String>(); � The data in the collection are the rails names.add(“Jan”); names.add(“Levi”); � Iterator created at the far left post names.add(“Tom”); � As long as a rail exists to the right of the As long as a rail exists to the right of the names.add( Jose ); names add(“Jose”); Iterator<String> it = names.iterator(); Iterator, hasNext() is true int i = 0; iterator object j “Jan” “Levi” “Tom” “Jose” CS 307 Fundamentals of CS 307 Fundamentals of 13 14 Computer Science Iterators Computer Science Iterators Fence Analogy Fence Analogy while( it.hasNext() ) { i i while( it.hasNext() ) { i i i++; i++; System.out.println( it.next() ); System.out.println( it.next() ); } } // when i == 1, prints out Jan // when i == 2, prints out Levi first call to next moves iterator to next post and returns “Jan” next post and returns “Jan” “Jan” “Levi” “Tom” “Jose” “Jan” “Levi” “Tom” “Jose” CS 307 Fundamentals of 15 CS 307 Fundamentals of 16 Computer Science Iterators Computer Science Iterators

  5. Fence Analogy Fence Analogy while( it.hasNext() ) { i i while( it.hasNext() ) { i i i++; i++; System.out.println( it.next() ); System.out.println( it.next() ); } } // when i == 3, prints out Tom // when i == 4, prints out Jose “Jan” “Levi” “Tom” “Jose” “Jan” “Levi” “Tom” “Jose” CS 307 Fundamentals of CS 307 Fundamentals of 17 18 Computer Science Iterators Computer Science Iterators Fence Analogy Attendance Question 3 while( it.hasNext() ) { i i � What is output by the following code? i++; ArrayList<Integer> list; List = new ArrayList<Integer>(); System.out.println( it.next() ); list.add(3); } list add(3); list.add(3); // call to hasNext returns false list.add(5); // while loop stops Iterator<Integer> it = list.iterator(); g (); System.out.println(it.next()); “Jan” “Levi” “Tom” “Jose” System.out.println(it.next()); B. 5 C. 3 3 5 A.3 D 3 3 D. 3 3 E 3 5 E. 3 5 CS 307 Fundamentals of 19 CS 307 Fundamentals of 20 Computer Science Iterators Computer Science Iterators

  6. Comodification remove method � If � If a Collection ( ArrayList ) is changed � Can use the Iterator to remove things from the � C th t thi f th ( ) i h d Collection while an iteration via an iterator is in progress � Can onl be called once per call to � Can only be called once per call to next() t() an Exception will be thrown the next time the public void removeWordsOfLength(int len) { next() or remove() methods are called String temp; g p via the iterator Iterator it = myList.iterator ArrayList<String> names = while( it.hasNext() ) { temp = (String)it.next(); temp (String)it.next(); new ArrayList<String>(); i i () if( temp.length() == len ) names.add(“Jan”); it.remove(); Iterator<String> it = names.iterator(); Iterator<String> it = names iterator(); } } names.add(“Andy”); // original list = [“dog”, “cat”, “hat”, “sat”] it.next(); // exception will occur here (); // p // resulting list after removeWordsOfLength(3) ? 3 CS 307 Fundamentals of CS 307 Fundamentals of 21 22 Computer Science Iterators Computer Science Iterators Common Iterator Error The Iterable Interface � A related interface is Iterable � A public void printAllOfLength(ArrayList<String> names, bli id i tAllOfL th(A Li t<St i > l t d i t f i bl int len) � One method in the interface: { //pre: names != null, names only contains Strings public Iterator<T> iterator() //post: print out all elements of names equal in // length to len � Why? Iterator<String> it = names.iterator(); te ato St g t a es. te ato (); � Anything that implements the Iterable while( it.hasNext() ){ interface can be used in the for each loop. if( it.next().length() == len ) System.out.println( it.next() ); S t t i tl ( it t() ) ArrayList<Integer> list; } //code to create and fill list } int total = 0; int total = 0; // given names = [“Jan”, “Ivan”, “Tom”, “George”] for( int x : list ) // and len = 3 what is output? total += x; CS 307 Fundamentals of 23 CS 307 Fundamentals of 24 Computer Science Iterators Computer Science Iterators

Recommend


More recommend