140 likes | 250 Views
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
E N D
Iterators(partial) Chapter 8 Slides by Nadia Al-Ghreimil adopted from Steve ArmstrongLeTourneau 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 … 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 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
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()); nameIterator.remove(); IllegalStateException Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Using iterators 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()); nameIterator.remove(); nameList.display(); 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 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) Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X