1 / 14

Iterators (partial)

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

svea
Download Presentation

Iterators (partial)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Iterators(partial) Chapter 8 Slides by Nadia Al-Ghreimil adopted from Steve ArmstrongLeTourneau 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. 0-13-237045-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 … Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-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. 0-13-237045-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 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

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

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

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

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

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

  13. 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

  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) Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

More Related