Iterators partial
This presentation is the property of its rightful owner.
Sponsored Links
1 / 14

Iterators (partial) PowerPoint PPT Presentation


  • 46 Views
  • Uploaded on
  • Presentation posted in: General

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

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Iterators partial

Iterators(partial)

Chapter 8

Slides by Nadia Al-Ghreimil

adopted from Steve ArmstrongLeTourneau University Longview, TX

ã 2007,Prentice Hall


Chapter contents

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

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

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

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

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 iterators1

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

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 iterator1

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

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 iterator1

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

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 implementation1

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

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


  • Login