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

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture22.
Bag implementation Add(T item) – Enlarge bag if necessary; allocate larger array Remove(T item) – Reduce bag if necessary; allocate smaller array Iterator.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
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.
Topic 6 Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 1 Topic 12 ADTS, Data Structures, Java Collections and Generic Data Structures.
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.
Iterators CS 367 – Introduction to Data Structures.
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
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
The Java Collections Framework By the end of this lecture you should be able to: use the ArrayList class to store a list of objects; use the HashSet class.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
1 Iterators "First things first, but not necessarily in that order " -Dr. Who CS Computer Science II.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Java Generics Compiled from Core Java Technologies Tech Tips By Billy B. L. Lim.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
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.
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.
Comp1004: Environments The Java Library. Coming up Recap – Encapsulation – Constructors – Loops – Arrays – ArrayList – Iterators The Java Library – Implementation.
Functional Processing of Collections (Advanced) 6.0.
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Linked Data Structures
EKT472: Object Oriented Programming
Iterators.
Objects First with Java CITS1001 week 4
EECE 310: Software Engineering
Software Development Iterators
ADT’s, Collections/Generics and Iterators
Functional Processing of Collections (Advanced)
Topic 6 Generic Data Structures
Top Ten Words that Almost Rhyme with “Peas”
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
08/15/09 Design Patterns James Brucker.
Generics 27-Nov-18.
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
"First things first, but not necessarily in that order." -Dr. Who
Object Oriented Programming in java
Collections James Brucker.
Interator and Iterable
"First things first, but not necessarily in that order " -Dr. Who
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
slides created by Alyssa Harding
Generics 2-May-19.
Iterators Dan Fleck.
Presentation transcript:

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

Iterators ArrayList is part of the Java Collections Framework Collection is an interface that specifies the basic operations every collection (data structure) should have Some Collections don’t have a definite order Sets, Maps, Graphs How to access all the items in a Collection with no specified order? CS314 Iterators

Iterator Interface An iterator object is a “one shot” object it is designed to go through all the elements of a Collection once if you want to go through the elements of a Collection again you have to get another iterator object Iterators are obtained by calling a method from the Collection CS314 Iterators

Iterator Iterface Methods The Iterator interface specifies 3 methods: boolean hasNext() //returns true if this iteration has more elements E next() //returns the next element in this iteration //pre: hastNext() void remove() /*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 Iterators

Clicker Question 1 Which of the following produces a syntax error? 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 CS314 Iterators

Iterator Imagine a fence made up of fence posts and rail sections CS314 Iterators

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 CS314 Iterators

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” CS314 Iterators

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” CS314 Iterators

Fence Analogy while( it.hasNext() ) { i++; System.out.println( it.next() ); } // when i == 2, prints out Levi “Jan” “Levi” “Tom” “Jose” CS314 Iterators

Fence Analogy while( it.hasNext() ) { i++; System.out.println( it.next() ); } // when i == 3, prints out Tom “Jan” “Levi” “Tom” “Jose” CS314 Iterators

Fence Analogy while( it.hasNext() ) { i++; System.out.println( it.next() ); } // when i == 4, prints out Jose “Jan” “Levi” “Tom” “Jose” CS314 Iterators

Fence Analogy while( it.hasNext() ) { i++; System.out.println( it.next() ); } // call to hasNext returns false // while loop stops “Jan” “Levi” “Tom” “Jose” CS314 Iterators

Typical Iterator Pattern public void printAll(Collection<String> list) { Iterator<String> it = list.iterator(); while( it.hasNext() ) { String temp = it.next(); System.out.println( temp ); } CS314 Iterators

Clicker Question 2 What is output by the following code? ArrayList<Integer> list; list = new ArrayList<Integer>(); list.add(3); list.add(5); Iterator<Integer> it = list.iterator(); System.out.print(it.next() + " "); System.out.print(it.next()); 3 B. 3 5 C. 3 3 5 D. 3 3 E. 3 3 then a runtime error

remove method AnIterator can be used to remove things from the Collection Can only be called once per call to next() public void removeWordsOfLength(int len) { Iterator<String> it = myList.iterator while( it.hasNext() ) { String temp = it.next(); if(temp.length() == len) it.remove(); } // original list = [“dog”, “cat”, “hat”, “sat”] // resulting list after removeWordsOfLength(3) ? CS314 Iterators

Clicker Question 3 No output due to syntax error 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 CS314 Iterators

The Iterable Interface A related interface is Iterable One method in the interface: public Iterator<T> iterator() Why? Anything that implements the Iterable interface can be used in the for each loop. ArrayList<Integer> list; //code to create and fill list int total = 0; for( int x : list ) total += x; CS314 Iterators

Iterable If you simply want to go through all the elements of a Collection (or Iterable thing) use the for each loop hides creation of the Iterator public void printAllOfLength(ArrayList<String> names, int len){ //pre: names != null, names only contains Strings //post: print out all elements of names equal in // length to len for(String s : names){ if( s.length() == len ) System.out.println( s ); } CS314 Iterators

Implementing an Iterator Implement an Iterator for our GenericList class Nested Classes Inner Classes Example of encapsulation checking precondition on remove does our GenricList need an Iterator? CS314 Iterators

Comodification If a Collection (ArrayList) is changed while an iteration via an iterator is in progress an Exception will be thrown the next time the next() or remove() methods are called via the iterator ArrayList<String> names = new ArrayList<String>(); names.add(“Jan”); Iterator<String> it = names.iterator(); names.add(“Andy”); it.next(); // exception occurs here CS314 Iterators