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

Slides:



Advertisements
Similar presentations
Computer Science 209 Software Development Iterators.
Advertisements

Bag implementation Add(T item) – Enlarge bag if necessary; allocate larger array Remove(T item) – Reduce bag if necessary; allocate smaller array Iterator.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Grouping Objects 3 Iterators, collections and the while loop.
Generic Classes and 'for each' Loops List zoo = new ArrayList (); // … add several Animals to the zoo for (Animal animal : zoo) // do something with animal.
CS 280 Data Structures Professor John Peterson. Project 9 Questions? IS280.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 Topic 8 Iterators "First things first, but not necessarily in that order " -Dr. Who.
CS 307 Fundamentals of Computer ScienceIterators 1 Topic 14 Iterators "First things first, but not necessarily in that order " -Dr. Who.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Iterator COMP 401, Spring 2013 Lecture 07 1/31/2013.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
Arrays And ArrayLists - S. Kelly-Bootle
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
1 Iterators "First things first, but not necessarily in that order " -Dr. Who CS Computer Science II.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
CS261 – Data Structures Iterator ADT Dynamic Array and Linked List.
Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class,
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18.
List Interface and Linked List Mrs. Furman March 25, 2010.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Part 1: Composition, Aggregation, and Delegation Part 2: Iterator COMP 401 Fall 2014 Lecture 10 9/18/2014.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
EKT472: Object Oriented Programming
Iterators.
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
EECE 310: Software Engineering
Chapter 4 Repetition Statements (loops)
Software Development Iterators
ADT’s, Collections/Generics and Iterators
Sequences and Iterators
Lecture 15: More Iterators
Loop Structures.
Top Ten Words that Almost Rhyme with “Peas”
Iteration Abstraction
In-Class Extended Example Ch. 6.4
Generics A Brief Review 16-Nov-18.
Outline Altering flow of control Boolean expressions
"First things first, but not necessarily in that order " -Dr. Who
Generics 27-Nov-18.
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Copyright ©2012 by Pearson Education, Inc. All rights reserved
slides adapted from Marty Stepp
Building Java Programs
Collections James Brucker.
Interator and Iterable
"First things first, but not necessarily in that order " -Dr. Who
JCF Collection classes and interfaces
"First things first, but not necessarily in that order " -Dr. Who
slides created by Alyssa Harding
slides created by Alyssa Harding
Generics 2-May-19.
List Interface ArrayList class implements the List Interface
Iterators Dan Fleck.
Presentation transcript:

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

CS 221 - Computer Science II 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

Iterator Interface Methods The Iterator interface lists 3 methods: // Returns true if this iteration has more elements. boolean hasNext() // Returns the next element in this iteration. // Precondition: hasNext returns true T next() /* Optional method. Removes the last element returned by the Iterator. Precondition: Can only be called only once for each call to next. After calling, must call next again before calling remove again. */ void remove() CS 221 - Computer Science II

CS 221 - Computer Science II 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

CS 221 - Computer Science II 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

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

CS 221 - Computer Science II 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

CS 221 - Computer Science II 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

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

CS 221 - Computer Science II 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

CS 221 - Computer Science II 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

CS 221 - Computer Science II 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

CS 221 - Computer Science II 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

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

CS 221 - Computer Science II Question 2 What is output of the following code? ArrayList<Integer> list; list = new ArrayList<Integer>(); list.add(3); list.add(5); Iterator<Integer> it = list.iterator(); System.out.println(it.next()); A. 3 B. 5 C. 3 3 5 D. 3 3 E. 3 5 F. 5 3 CS 221 - Computer Science II

CS 221 - Computer Science II Question 2 What is output of the following code? ArrayList<Integer> list; list = new ArrayList<Integer>(); list.add(3); list.add(5); Iterator<Integer> it = list.iterator(); System.out.println(it.next()); A. 3 B. 5 C. 3 3 5 D. 3 3 OR E. 3 5 OR F. 5 3 CS 221 - Computer Science II

CS 221 - Computer Science II remove method An Iterator can also be used to remove things from an ADT. Precondition: Can only be called once for each call to next. public void removeWords(Iterable<String> myList, int len) { 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

CS 221 - Computer Science II Question 3 public void printTarget(ArrayList<String> names, int len) { 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

CS 221 - Computer Science II Question 3 public void printTarget(ArrayList<String> names, int len) { 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

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

CS 221 - Computer Science II Using Iterable ADTs But if you simply want to go through all the elements of an Iterable ADT, use for-each loop. Hides creation of the Iterator. But can’t modify as traverse list. public void doSomething(Iterable<T> list) { for(T temp : list) // do something with temp } CS 221 - Computer Science II

CS 221 - Computer Science II Using Iterable ADTs 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 } But other methods of iterating through ADT are easier to implement. CS 221 - Computer Science II

Implementing an Iterator Nested / Inner Class Example of encapsulation. Key Implementation Details Does your ADT need an Iterator? How keep track of “location” in ADT? Have to check whether ADT has been modified since Iterator has been instantiated ConcurrentModificationException How check precondition on remove method? CS 221 - Computer Science II

CS 221 - Computer Science II Comodification If a ADT with an Iterator is changed after it’s been instantiated, a ConcurrentModificationException should 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

CS 221 - Computer Science II