Iterators (short version) Chapter 15 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Slides:



Advertisements
Similar presentations
Lists Chapter 8 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Advertisements

Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
The Efficiency of Algorithms Chapter 4 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Stacks Chapter 21 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
An Introduction to Sorting Chapter 8 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A List Implementation That Links Data Chapter 6 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.
Completing the Linked Implementation of a List Chapter 7 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.
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.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Tree Implementations Chapter 24 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Graph Implementations Chapter 29 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Graph Implementations Chapter 31 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.
Implementations of the ADT Stack Chapter 7 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures and Abstractions with Java, 4e Frank Carrano
An Introduction to Sorting Chapter 8 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Lists Chapter 4 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
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.
Iterator Summary Slides by Entesar Al-Mosallam adopted from Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
An Introduction to Sorting
A Bag Implementation that Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Tree Implementations (plus briefing about Iterators)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Graph Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Iterators (short version)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Slides by Steve Armstrong LeTourneau University Longview, TX
Iterators (partial) Chapter 8 Slides by Nadia Al-Ghreimil
Dynamic Data Structures and Generics
List Implementations that Use Arrays
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack Implementations
Dynamic Data Structures and Generics
Copyright ©2012 by Pearson Education, Inc. All rights reserved
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
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 Uses An Array
A List Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

Iterators (short version) Chapter 15 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Contents What Is an Iterator? The Interface Iterator  Using the Interface Iterator An Inner Class Iterator  A Linked Implementation  An Array-Based Implementation Why Are Iterator Methods in Their Own Class? Copyright ©2012 by Pearson Education, Inc. All rights reserved

Objectives Describe concept of iterator Use iterator to traverse, manipulate a list Implement in Java inner class iterator for list Describe pros of inner class iterators Copyright ©2012 by Pearson Education, Inc. All rights reserved

What Is an Iterator? Program component  Enables you to step through, or  Traverse, a collection of data During a traversal  Each data item considered once When we write loops  They traverse, iterate through whole list Consider interface Iterator, Listing 15-1Listing 15-1 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Using the Interface Iterator Implement iterator methods within their own class  Can be private, separate from ADT class  Or private, inner class of ADT Copyright ©2012 by Pearson Education, Inc. All rights reserved

Using iterators ListWithIteratorInterface nameList = new ArrayListWithIterator (); nameList.add(“Maha”); nameList.add(“Nadia”); nameList.add(“Rehab”); Iterator nameIterator = nameList.getIterator(); nameIterator.remove(); if(nameIterator.hasNext();) nameIterator.next(); System.out.println (nameIterator.next()); nameIterator.remove(); nameListnameIterator Maha Nadia Rehab Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X IllegalStateException

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

Multiple Iterators Possible to have multiple iterators of the same list in progress simultaneously View code which counts number of times that Jane appears in a list of names Listing 15-A Listing 15-A Output of Listing 15-AOutput Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 15-4 Counting the number of times that Jane appears in a list of names

Linked Implementation Define the methods specified in Iterator within new inner class  A class that implements the ADT list  Needs another method that client can use to create an iterator Copyright ©2012 by Pearson Education, Inc. All rights reserved

Linked Implementation New interface needed Listing 15-3 Listing 15-4, an outline of the class LinkedListWithIteratorListing 15-4 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 15-7 An inner class iterator with direct access to the linked chain that implements the ADT Copyright ©2012 by Pearson Education, Inc. All rights reserved

Array-Based Implementation Iterator will support the remove method Adaptation of Alist class, Chapter 13 Listing 15-5  Implements interface ListWithIteratorInterface  Also includes method getIterator  Contains the inner class IteratorForArrayList, implements interface Iterator. Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 15-8 The array of list entries and nextIndex (a) just before the call to next ; (b) just after the call to next but before the call to remove ; (c) after the call to remove Copyright ©2012 by Pearson Education, Inc. All rights reserved

Why Are Iterator Methods in Their Own Class? Inner class iterators have direct access to structure containing ADT’s data Can execute fast More than one traversal can be in progress at a time. Copyright ©2012 by Pearson Education, Inc. All rights reserved

End Chapter 15 Copyright ©2012 by Pearson Education, Inc. All rights reserved