1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.

Slides:



Advertisements
Similar presentations
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Double-Linked Lists and Circular Lists
The List ADT Textbook Sections
Computer Science 209 Software Development Iterators.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Iterators Chapter 8 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Iterators CS 367 – Introduction to Data Structures.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Information and Computer Sciences University of Hawaii, Manoa
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Computer Science 209 The Factory Pattern. Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class,
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
List Interface and Linked List Mrs. Furman March 25, 2010.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
Iterators. Chapter Scope The purpose of an iterator The Iterator and Interable interfaces The concept of fail-fast collections Using iterators to solve.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
The Queue ADT.
Iterators.
Stack: a Linked Implementation
The List ADT.
EECE 310: Software Engineering
CSE 373 Implementing a Stack/Queue as a Linked List
The List ADT Reading: Textbook Sections 3.1 – 3.5
Linked Lists Chapter 5 (continued)
Queues Rem Collier Room A1.02
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
COMP 121 Week 9: ArrayList.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Java Software Structures: John Lewis & Joseph Chase
The List ADT Reading: Textbook Sections 3.1 – 3.5
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Copyright ©2012 by Pearson Education, Inc. All rights reserved
slides adapted from Marty Stepp
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Dynamic Data Structures and Generics
Example: LinkedSet<T>
Computer Science and Engineering
Chapter 4 Queues.
Linked Lists Chapter 5 (continued)
CSC 143 Java Linked Lists.
Programming II (CS300) Chapter 07: Linked Lists
Iterators Dan Fleck.
TCSS 143, Autumn 2004 Lecture Notes
CSE 143 Lecture 21 Advanced List Implementation
Linked Lists Chapter 5 (continued)
Java Generics & Iterators
The List Container and Iterators
CHAPTER 3: Collections—Stacks
Presentation transcript:

1 Iterators & the Collection Classes

2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type of collections like an ArrayList » These classes implements the List interface

3 There are 35 methods in the Collections Interface. Several of the methods have to do with the iterator interface.

4 » A key operation of a Collections class is the necessity to traverse and process the data stored in the collection » Therefore, most Collection classes e.g the ArrayList class, implement a special Interface that provides for this – » the Iterator interface.

5 » In order to step through a collection in the past we used loops. » The question is : How can any Collection class allow users to loop through say an ArrayList without making sure they do not corrupt the data? » The solution is the use of iterators.

6 Iterator An iterator is an object that enables a user to loop through a collection.

7 » So associated with each class that implements the Collections interface there is an Iterator class. » Each iterator class must implement the iterator interface which consists of methods that we will examine.

8 The following methods are listed in the iterator interface; public interface iterator { T next(); boolean hasnext(); void remove() throws unsupportedException() }

9

10

11

12  The ArrayList class ( and many others) have a nested class that implements the Iterator interface  The nested class -  The nested class - ArrayIterator implements the iterator interface..  Let’s look at the code:

13 import java.util.Iterator; // using the iterator in java. util public class ArrayList implements ListADT protected T[] list; public ArrayList() { rear = 0; list = (T[])(new Object[DEFAULT_CAPACITY]); } import java.util.Iterator; // using the iterator in java. util public class ArrayList implements ListADT { protected final int DEFAULT_CAPACITY = 100; private final int NOT_FOUND = -1; protected int rear; protected T[] list; // // Creates an empty list using the default capacity. // public ArrayList() { rear = 0; list = (T[])(new Object[DEFAULT_CAPACITY]); }

14 other Methods add and remove in Arraylist class etc. // method to create an object of ArrayIterator inner class public ArrayIterator iterator() new ArrayIterator iterator() { return new ArrayIterator ( list, rear ); } Note that we pass rear ( rear - stores the index of the last item in the array) ( rear - stores the index of the last item in the array) And list which is the array of T holding the data } And list which is the array of T holding the data }

15

current COUNT

current COUNT

18

19 Look at this code in the ArrayList class: // Pre: sends references to the arraylist object (1) ”list” and (2) the number of items (1) ”list” and (2) the number of items // Postcondition: an iterator to traverse the arraylist has been returned the main class /************************************************************************************************ /************************************************************************************************ public Iterator iterator( ) { return new ArrayInterator(list, count ); } // method iterator /****************************************** This method creates an object of the inner iterator class ArrayInterator( variables); and returns it to the main application and returns it to the main application

20

21

22

23 public Iterator iterator( ) { return new ArrayInterator(list, count ); } // method iterator Inside the ArrayList class, we have a method to create the array iterator In DatingService when the find button is pressed we created an iterator: “List” is an object for the Arraylist of Clients “List” is an object for the Arraylist of Clients if (e.getActionCommand().equals("FIND")) { // code to get an iterator Iterator iter = List.iterator(); if (e.getActionCommand().equals("FIND")) { // code to get an iterator Iterator iter = List.iterator();

24 » For the Linked List class, the nested iterator class – » class ListIterator class - will have one field: ListNode cur; // used to traverse list

25 class LinkedIterator implements Iterator { private int count; // the number of elements in the collection private LinearNode curr; // the current position // // Sets up this iterator using the specified items. // first will be equal to head, size is # of items currently in list // public LinkedIterator (LinearNode head, int size) { curr = head; // where head points to first node count = size; }

26 // method hasNext() returns true as long as //next is not null - allows the iteration over //the collection to continue if there are more //items to process public boolean hasNext() { return cur != null; }

27

28

29 raydoemenull head Data next cur Variable to traverse the list Pointer to next node in the list

30 raydoemenull head cur Data next Cur traverses the list

31

32

33

34

35

36

class LinkedIterator implements Iterator private int count; // the number of elements in the collection private LinearNode cur; // the current position public LinkedIterator(LinerarNode first, size ) { cur = first; // sets up cut to point to the beginning of the list – first count -= size; } // constructor //************************************************************* // Precondition: this Iterator is positioned at an element in this Linked Collection. // Postcondition: The element this Iterator was (before this call) positioned at has been //returned, and this Iterator has advanced. //************************************************************* public T next() { T element = cur.data; // get the data cur = cur.getNext(); // advance to the next node return element; // return the element } // method next //************************************************************* // Postcondition: true has been returned if this Iterator is positioned // an element in this Collection. Otherwise, false has been returned. //************************************************************* public boolean hasNext() { return cur != null; // determines if there are more items to processs } // method hasNext //************************************************************* public void remove() { throws unsupported Exception

39 » There is also a ListIterator interface which extends Iterator. There is also a ListIterator interface which extends Iterator. » The description is at this link: The description is at this link: » » It is in java.util and has more methods than the parent interface Iterator

40 » Interface ListIterator, which allows a programmer to traverse a List in either direction and make modifications to the underlying List » -- Newer versions of the iterator:

41 » Some additional methods in ListIterator: » hasPrevious() returns true if there are more elements in a backward direction » previous() returns the previous element in the List » previousIndex() returns the index of the previous element in the list.

43 » New with JDK 1.5 is the Iterable interface. It resides in java.lang and has some enhancements » What are the differences between the two (class that implements Iterable and a class that implements Iterator)? » We can make an iterable object have different kinds of iterators?

44 » class IterableIterator implements Iterable » { » private Iterator iter; » public IterableIterator(Iterator iter) » { this.iter = iter; } » public Iterator iterator() » { return iter; » } » Allows different types of iterators to be created with different capabilities

45 class Tree // creates two iterators { IterableIterator depthFirstIterator(); IterableIterator breadthFirstIterator(); }

46 » But then, how does this work when we use the iterable object in a foreach statement? » How do we specfify which iterator we want to use? » for (Node node: aTree.depthFirstIterator()){}

47 » And by the way, the only benefit of the previous example is to be able to use a for loop on an iterable collection. »

48 » In addition, for now the Iterable interface is not backward compatible.