1 / 24

Iterators

Iterators. "First things first, but not necessarily in that order " -Dr. Who. Iterator Interface. An iterator object is a “one shot” object it is designed to go through all the elements of an ADT once

zoll
Download Presentation

Iterators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Iterators "First things first, but not necessarily in that order " -Dr. Who

  2. Iterator Interface • 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 • Iterators are obtained by calling the iterator method CS 221 - Computer Science II

  3. Iterator Interface Methods • The Iterator interface lists 3 methods: boolean hasNext() //returns true if this iteration has more elements T next() //returns the next element in this iteration //pre: hasNext 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

  4. Question 1 • Which of the following will not compile? ArrayList<String> list = new ArrayList<String>(); Iterator<String> it1 = new Iterator(); // I Iterator<String> it2 = new Iterator(list); // II Iterator<String> it3 = list.iterator(); // III A. I B. II C. III D. I and II E. II and III CS 221 - Computer Science II

  5. Question 1 • Which of the following will not compile? ArrayList<String> list = new ArrayList<String>(); Iterator<String> it1 = new Iterator(); // I Iterator<String> it2 = new Iterator(list); // II Iterator<String> it3 = list.iterator(); // III A. I B. II C. III D. I and II E. II and III CS 221 - Computer Science II

  6. Visualizing an Iterator • Imagine a fence made up of fence posts and rail sections rails fenceposts CS 221 - Computer Science II

  7. Fence Analogy • The iterator lives on the fence posts • The data in the collection are the rails • Iterator created at the far left post • As long as a rail exists to the right of the iterator, hasNext() is true iterator object CS 221 - Computer Science II

  8. Fence Analogy ArrayList<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 221 - Computer Science II

  9. Fence Analogy while( it.hasNext() ) { i++; System.out.println( it.next() ); } // when i == 1, prints out Jan first call to next() moves iterator tonext post and returns “Jan” “Jan” “Levi” “Tom” “Jose” CS 221 - Computer Science II

  10. Fence Analogy while( it.hasNext() ) { i++; System.out.println( it.next() ); } // when i == 2, prints out Levi “Jan” “Levi” “Tom” “Jose” CS 221 - Computer Science II

  11. Fence Analogy while( it.hasNext() ) { i++; System.out.println( it.next() ); } // when i == 3, prints out Tom “Jan” “Levi” “Tom” “Jose” CS 221 - Computer Science II

  12. Fence Analogy while( it.hasNext() ) { i++; System.out.println( it.next() ); } // when i == 4, prints out Jose “Jan” “Levi” “Tom” “Jose” CS 221 - Computer Science II

  13. Fence Analogy while( it.hasNext() ) { i++; System.out.println( it.next() ); } // call to hasNext returns false // while loop stops “Jan” “Levi” “Tom” “Jose” CS 221 - Computer Science II

  14. Typical Iterator Pattern Iterator<T> it = list.iterator(); while( it.hasNext() ) { T temp = it.next(); // do something with temp } CS 221 - Computer Science II

  15. Question 2 • What is output of 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 5 D. 3 3 E. 3 5 CS 221 - Computer Science II

  16. Question 2 • What is output of 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 5 D. 3 3OR E. 3 5OR F. 5 3 CS 221 - Computer Science II

  17. remove method • An Iterator an be used to remove things from an ADT • Can only be called once for each call to next public void removeWords(Iterable<String> myList, intlen) { Iterator<String> it = myList.iterator while( it.hasNext() ) { String temp = it.next(); if(temp.length() == len)it.remove(); } } Given list= [“dog”, “fish”, “cat”, “gerbil”], what is resulting list after call to removeWords(3)? CS 221 - Computer Science II

  18. Question 3 public void printTarget(ArrayList<String> names, intlen) { Iterator<String> it = names.iterator(); while( it.hasNext() ) if( it.next().length() == len ) System.out.println( it.next() ); } Given names = [“Jan”, “Ivan”, “Tom”, “George”] and len = 3, what is output by the printTarget method? • 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. Question 3 public void printTarget(ArrayList<String> names, intlen) { Iterator<String> it = names.iterator(); while( it.hasNext() ) if( it.next().length() == len ) System.out.println( it.next() ); } Given names = [“Jan”, “Ivan”, “Tom”, “George”] and len = 3, what is output by the printTarget method? • 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

  20. The Iterable Interface • An interface implemented by an ADT • One method in the interface: public Iterator<T> iterator() • Can use typical iterator pattern to access all elements in ADT: public void doSomething(Iterable<T> list) Iterator<T> it = list.iterator(); while( it.hasNext() ) { T temp = it.next(); // do something with temp } } CS 221 - Computer Science II

  21. Using IterableADTs • 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 doSomething(Iterable<T> list) { for(T temp : list) { // do something with temp } } CS 221 - Computer Science II

  22. Using IterableADTs • Can also use typical for loop public void doSomething(Iterable<T> list) { for (Iterator<T> itr = list.iterator(); itr.hasNext(); ) { T temp = iter.next(); // do something with temp } } CS 221 - Computer Science II

  23. Implementing an Iterator • Nested / Inner Class • Example of encapsulation • 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

  24. Comodification • If a ADT with an Iterator is changed after it’s been instantiated, a ConcurrentModificationException will be thrown the next time call next or remove methods are called ArrayList<String> names = new ArrayList<String>(); names.add(“Jan”); Iterator<String> it = names.iterator(); names.add(“Andy”); it.next(); // exception will occur here CS 221 - Computer Science II

More Related