Iterators Topic 8 ArrayList is part of the Java Collections Iterators Framework Collection is an interface that specifies the "First things first, but not necessarily basic operations every collection (data in that order " structure) should have -Dr. Who Sets, Maps, Graphs How to access all the items in a Collection with no specified order? 1 CS314 2 Iterators Iterator Interface Iterator Iterface Methods The Iterator interface 3 methods we will use: An iterator object is a "one shot" object it is designed to go through all the //returns true if this iteration has more elements elements of a Collection once if you want to go through the elements of a Collection again you //returns the next element in this iteration have to get another iterator object //pre: hastNext() Iterators are obtained by calling 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. After calling, must call next again before calling remove again. */ CS314 3 CS314 4 Iterators Iterators
Clicker Question 1 Iterator Which of the following produces a syntax error? Imagine a fence made up of fence posts and rail sections rails A. B. C. D. fenceposts E. CS314 5 CS314 6 Iterators Iterators Fence Analogy 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 "Jan" "Levi" "Tom" "Jose" CS314 7 CS314 8 Iterators Iterators
Fence Analogy Fence Analogy first call to next moves iterator to next post and returns "Jan" "Jan" "Levi" "Tom" "Jose" "Jan" "Levi" "Tom" "Jose" CS314 9 CS314 10 Iterators Iterators Fence Analogy Fence Analogy "Jan" "Levi" "Tom" "Jose" "Jan" "Levi" "Tom" "Jose" CS314 11 CS314 12 Iterators Iterators
Fence Analogy Typical Iterator Pattern "Jan" "Levi" "Tom" "Jose" CS314 13 CS314 14 Iterators Iterators Clicker Question 2 remove method What is output by the following code? An can be used to remove things from the Can only be called once per call to A. B. C. D. E. then a runtime error CS314 16 Iterators
Clicker Question 3 The Iterable Interface A related interface is One method in the interface: Why? Anything that implements the interface can be used in the for each loop. Given names = ["Jan", "Ivan", "Tom", "George"] and len = 3 what is output by the printTarget method? A. Jan Ivan Tom George B. Jan Tom C. Ivan George D. No output due to syntax error E. No output due to runtime error 17 CS314 18 Iterators Iterable Implementing an Iterator If you simply want to go through all the Implement an Iterator for our GenericList elements of a Collection (or Iterable thing) class use the for each loop Nested Classes hides creation of the Iterator Inner Classes Example of encapsulation checking precondition on remove does our GenricList need an Iterator? CS314 19 CS314 20 Iterators Iterators Madilyn L. 2019
Comodification If a ( ) is changed while an iteration via an iterator is in progress an Exception will be thrown the next time the or methods are called via the iterator CS314 21 Iterators
Recommend
More recommend