Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Iterators (short version) Chapter 15 Copyright ©2012 by Pearson Education, Inc. All rights reserved."— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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. 0-13-237045-X IllegalStateException

7 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. 0-13-237045-X

8 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

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

10 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

11 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

12 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

13 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

14 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

15 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

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


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

Similar presentations


Ads by Google