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

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
An Introduction to Sorting Chapter 8 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Iterators Chapter 8 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.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Bag Implementations that Use Arrays Chapter 2 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures and Abstractions with Java, 4e Frank Carrano
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
© 2011 Pearson Addison-Wesley. All rights reserved 9 B-1 Chapter 9 (continued) Advanced Java Topics.
© 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.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
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.
Data Abstraction: The Walls
Linked Lists Chapter 5 (continued)
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Bag Implementations that Use Arrays
An Introduction to Sorting
A Bag Implementation that Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Advanced Java Topics Chapter 9 (continued)
Tree Implementations (plus briefing about Iterators)
A Bag Implementation that Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 12 Sorted Lists and their Implementations
Graph Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Hashing as a Dictionary Implementation
Iterators (short version)
Adapted from Pearson Education, Inc.
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
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Dynamic Data Structures and Generics
Linked Lists Chapter 5 (continued)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Iterators Dan Fleck.
Linked Lists Chapter 5 (continued)
Java Generics & Iterators
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
List Implementations that Use Arrays
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
A List Implementation that Links Data
Presentation transcript:

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

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents What Is an Iterator? The Interface Iterator Using the Interface Iterator A Separate Class 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

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents The Interface ListIterator Using the Interface ListIterator An Array-Based Implementation of the Interface ListIterator The Inner Class Java Class Library: The Interface Iterable Iterable and for-each Loops The Interface List Revisited Copyright ©2012 by Pearson Education, Inc. All rights reserved

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

What Is an Iterator? Program component During a traversal 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-1 Note: Code listing files must be in same folder as PowerPoint files for links to work Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 15-1 The effect of a call to next on a list iterator 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 Consider list of strings ListInterface<String> nameList = new LList<String>(); nameList.add("Jamie"); nameList.add("Joey"); nameList.add("Rachel"); Copyright ©2012 by Pearson Education, Inc. All rights reserved

Using the Interface Iterator Create an instance of SeparateIterator Iterator<String> nameIterator = new SeparateIterator<String>(nameList); Connects the iterator nameIterator to the list nameList Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 15-2 The effect of the iterator methods hasNext and next on a list Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 15-2 The effect of the iterator methods hasNext and next on a list Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 15-3 The effect of the iterator methods next and remove on a list Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 15-3 The effect of the iterator methods next and remove on a list Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved 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 Output of Listing 15-A Copyright ©2012 by Pearson Education, Inc. All rights reserved

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 Copyright ©2012 by Pearson Education, Inc. All rights reserved

A Separate Class Iterator Implementation of class SeparateIterator Implements the interface java.util.Iterator View source code, Listing 15-2 Note: definition of SeparateIterator independent of a particular implementation Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 15-5 A separate class iterator with a reference to an ADT, an indicator of its position within the iteration, and no knowledge of the ADT’s implementation Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 15-6 A list and nextPosition (a) just before the call to next; Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 15-6 A list and nextPosition (b) just after the call to next but before the call to remove Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 15-6 A list and nextPosition (c) after the call to remove Copyright ©2012 by Pearson Education, Inc. All rights reserved

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 LinkedListWithIterator Copyright ©2012 by Pearson Education, Inc. All rights reserved

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

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 Execute faster than separate class iterators Consider Listing 15-6 Modified linked implementation Differences with Listing 15-4 highlighted Copyright ©2012 by Pearson Education, Inc. All rights reserved

Why Are Iterator Methods in Their Own Class? Consider this traversal Quick traversal, but … Only one traversal can be in progress at a time Resulting ADT has too many operations Copyright ©2012 by Pearson Education, Inc. All rights reserved

The Interface ListIterator A second interface for iterators Listing 15-7 Extends Iterator Includes methods hasNext, next, and remove Copyright ©2012 by Pearson Education, Inc. All rights reserved

The Interface ListIterator Methods remove, add, and set are optional Can choose not to provide Must have an implementation that throws exception UnsupportedOperationException Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 15-9 The effect of a call to previous on a list Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 15-10 The indices returned by the methods nextIndex and previousIndex Copyright ©2012 by Pearson Education, Inc. All rights reserved

Using Interface ListIterator Given Interface ListIterator implemented as inner class of class that implements ADT list. Iterator includes the operations add, remove, and set. Method getIterator is added to ADT list. List nameList contains: Jess, Jim, Josh Iterator traverse is defined ListIterator<String> traverse = nameList.getIterator(); Copyright ©2012 by Pearson Education, Inc. All rights reserved

Using Interface ListIterator Statements Produce output Copyright ©2012 by Pearson Education, Inc. All rights reserved

Using Interface ListIterator Then, statements Produce output Copyright ©2012 by Pearson Education, Inc. All rights reserved

Using Interface ListIterator Finally, statements Produce output Copyright ©2012 by Pearson Education, Inc. All rights reserved

Array-Based Implementation of Interface ListIterator The interface ListWithListIteratorInterface Listing 15-8 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Array-Based Implementation of Interface ListIterator The class that implements the ADT list Listing 15-9 Class ArrayListWithListIterator Listing 15-10 Consider how remove and set will throw IllegalStateException. Happens when next or previous was not called, or remove or add has been called since the last call to next or previous Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 15-11 Possible contexts in which the method remove of the iterator traverse throws an exception when called Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 15-11 Possible contexts in which the method remove of the iterator traverse throws an exception when called Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 15-12 The array of list entries and nextIndex (a) just before the call to add; (b) just after the call to add Copyright ©2012 by Pearson Education, Inc. All rights reserved

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

Java Class Library: The Interface Iterable Listing 15-11 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Java Class Library: The Interface Iterable Listing 15-12 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Iterable and for-each Loops Can use a for-each loop to traverse Given Then Copyright ©2012 by Pearson Education, Inc. All rights reserved

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