Download presentation
Presentation is loading. Please wait.
Published byShona Small Modified over 8 years ago
1
1 Iterators & the Collection Classes
2
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
3 There are 35 methods in the Collections Interface. Several of the methods have to do with the iterator interface.
4
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
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
6 Iterator An iterator is an object that enables a user to loop through a collection.
7
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
8 The following methods are listed in the iterator interface; public interface iterator { T next(); boolean hasnext(); void remove() throws unsupportedException() }
9
9
10
10
11
11
12
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
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
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
15
16
16 8 2030 60908934 current COUNT
17
17 8 2030 60908934 current COUNT
18
18
19
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
20
21
21
22
22
23
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
24 » For the Linked List class, the nested iterator class – » class ListIterator class - will have one field: ListNode cur; // used to traverse list
25
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
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
27
28
28
29
29 raydoemenull head Data next cur Variable to traverse the list Pointer to next node in the list
30
30 raydoemenull head cur Data next Cur traverses the list
31
31
32
32
33
33
34
34
35
35
36
36
37
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
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: » http://www.janeg.ca/scjp/util/iterator.html http://www.janeg.ca/scjp/util/iterator.html » It is in java.util and has more methods than the parent interface Iterator
40
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
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
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
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
45 class Tree // creates two iterators { IterableIterator depthFirstIterator(); IterableIterator breadthFirstIterator(); }
46
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
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
48 » In addition, for now the Iterable interface is not backward compatible.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.