Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Iterators (partial) Chapter 8 Slides by Nadia Al-Ghreimil"— Presentation transcript:

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

2 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 X

3 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 X

4 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 X

5 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 X

6 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 X

7 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 X

8 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 X

9 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 X

10 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 X

11 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 X

12 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 X

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

14 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 X


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

Similar presentations


Ads by Google