Iterators (partial) Chapter 8 Slides by Nadia Al-Ghreimil

Slides:



Advertisements
Similar presentations
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Advertisements

Stacks Chapter 21 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
A List Implementation That Links Data Chapter 6 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Faster Sorting Methods Chapter 12 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Bag implementation Add(T item) – Enlarge bag if necessary; allocate larger array Remove(T item) – Reduce bag if necessary; allocate smaller array Iterator.
Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Announcements.
Iterators Chapter 8 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Queue, Deque, and Priority Queue Implementations Chapter 24 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Iterators Chapter 7. Chapter Contents What is an Iterator? A Basic Iterator Visits every item in a collection Knows if it has visited all items Doesn’t.
The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Stack Implementations Chapter 22 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Inheritance and Lists Chapter 14 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Searching Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
An Introduction to Sorting Chapter 11 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Hashing as a Dictionary Implementation Chapter 20 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Graph Implementations Chapter 31 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Java Classes Introduction and Chapter 1 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Dictionaries Chapter 17 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Lists Chapter 4 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Iterators (short version) Chapter 15 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Balanced Search Trees (partial) Chapter 29 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall - Edited by Nadia Al-Ghreimil.
Iterator Summary Slides by Entesar Al-Mosallam adopted from Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
Lists and Sorted Lists: various implementations Slides by Nadia Al-Ghreimil adopted from Steve Armstrong LeTourneau University Longview, TX  2007, 
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 4 Link Based Implementations
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Slides by Steve Armstrong LeTourneau University Longview, TX
Operator Overloading; Class string
Bag Implementations that Use Arrays
An Introduction to Sorting
Tree Implementations (plus briefing about Iterators)
Graph Implementations
Hashing as a Dictionary Implementation
Iterators (short version)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Slides by Steve Armstrong LeTourneau University Longview, TX
Slides by Steve Armstrong LeTourneau University Longview, TX
Slides by Steve Armstrong LeTourneau University Longview, TX
Dynamic Data Structures and Generics
Iteration Abstraction
List Implementations that Use Arrays
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Balanced Search Trees (partial)
Stack Implementations
Dynamic Data Structures and Generics
A List Implementation That Links Data
List Implementations that Use Arrays
Stack Implementations
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
A List Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

Iterators (partial) Chapter 8 Slides by Nadia Al-Ghreimil adopted from Steve Armstrong LeTourneau University Longview, TX ã 2007, Prentice Hall

Chapter Contents What is an Iterator? The Iinterface Iterator Using the Interface Iterator An Inner Class Iterator A Linked Implementation An Array-Based Implementation Why Are Iterator Methods in Their Own Class? Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

What Is an Iterator? A program component Enables you to step through, traverse a collection of data Can tell whether next entry exists Allows visiting all elements of a data structure: The iterator has to be connected to that data structure Iteration differs from traversal in that you move step by step Iterators may be manipulated Asked to advance to next entry Give a reference to current entry Modify the list as you traverse it (optional) etc … Mention here that in ListIterators you can move forward and backward!! Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Iterators in Java Interface Iterator hasNext Next remove (optional) Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

An Inner Class Iterator Define the iterator class as an inner class of the ADT Multiple iterations possible Has direct access to ADT's data fields as inner class We will look at a linked implementation without remove and an array implementation with remove Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

IllegalStateException Using iterators ListWithIteratorInterface <String> nameList = new ArrayListWithIterator <String>(); nameList.add(“Maha”); nameList.add(“Nadia”); nameList.add(“Rehab”); Iterator < String > nameIterator = nameList.getIterator(); nameIterator.remove(); if(nameIterator.hasNext();) nameIterator.next(); System.out.println (nameIterator.next()); nameList nameIterator Maha Nadia Rehab IllegalStateException As we go through this example, ask the students to look at the interface for iterators. Q - At the first remove, ask students they can predict what will happen. A - exception Q - Then ask them why it happened A - we did not call next() yet. At the second remove statement, point out that a call to next should give us “Rehab”, we have to make sure in our implementation that that will be the case. Ask the students where they think the arrow will be after removal? Only then click for next slide!! Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Using iterators nameList nameIterator Maha Rehab ListWithIteratorInterface <String> nameList = new LinkedListWithIterator <String>(); nameList.add(“Maha”); nameList.add(“Nadia”); nameList.add(“Rehab”); Iterator < String > nameIterator = nameList.getIterator(); nameIterator.remove(); if(nameIterator.hasNext();) nameIterator.next(); System.out.println (nameIterator.next()); nameList.display(); nameList nameIterator Maha Rehab Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Using the Interface Iterator Fig. 8-2 The effect of iterator methods on a list. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Using the Interface Iterator Fig. 8-3 The effect of the iterator methods next and remove on a list Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Multiple Iterators Possible to have several iterators of the same list View sample code Use my new code. Fig 8-4 Counting the number of times that Jane appears in a list of names Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

An Inner Class Iterator Fig. 8-7 An inner class iterator with direct access to the linked chain that implements the ADT Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

An Array-Based Implementation View outline of ArrayListWithIterator Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

An Array-Based Implementation Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Why are Iterator Methods in Their Own Class? Traversal methods do execute quickly Direct access to underlying data structure But … Only one traversal can be in progress at a time Resulting ADT requires too many operations (interface bloat) Direct the students to look at section ??? Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X