Presentation is loading. Please wait.

Presentation is loading. Please wait.

Iterators (short version)

Similar presentations


Presentation on theme: "Iterators (short version)"— Presentation transcript:

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

2 Copyright ©2012 by Pearson Education, Inc. All rights reserved
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 Copyright ©2012 by Pearson Education, Inc. All rights reserved
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 Copyright ©2012 by Pearson Education, Inc. All rights reserved
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-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 nameList nameIterator Maha Nadia Rehab
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 Copyright ©2012 by Pearson Education, Inc. All rights reserved
End Chapter 15 Copyright ©2012 by Pearson Education, Inc. All rights reserved


Download ppt "Iterators (short version)"

Similar presentations


Ads by Google