Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006.

Similar presentations


Presentation on theme: "Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006."— Presentation transcript:

1

2 Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006

3 List Traversal What is it? –A way of moving through linked data structures node by node –It can be used with plenty of classes, such as LinkedList, Set, etc.

4 Methods of Traversal Using the getNext() method of the ListNode class creating a current instance variable that refers to a particular node in a linked structure

5 But, There are some problems!!!!!!!!! For Example, what happens if you want to access the list in two places simultaneously?

6 The solution is... The Iterator!

7 So, What is an iterator? Iterator is an iterface in the java.util package. Iterators advance through the nodes of a linked data structure one by one

8 Methods of Iterator boolean hasNext() –returns true if there are more elements to be examined, false otherwise Object next() –returns the next element void remove() –removes the last element returned by next

9 hasNext() Current In this case hasNext() would return true

10 hasNext() Current In this case hasNext() would return false

11 next() The first call to next() results in the first element of the the data structure. To begin with, the list looks like this Current

12 next() When next is called, it looks like this Current

13 next() When next is called again, it looks like this Current

14 next() When next is called again, it looks like this Current If next is called one more time, an exception is thrown

15 remove() remove() can only be used once per call to next() next() must be called at least once before remove() is called If next() is not called before, an exception is thrown.

16 remove() If a the current node is position one (meaning next() would result in pos. 2) and remove() is called... Current 12 3

17 remove() The linked list would look like this Current 23

18 ListIterator ListIterator is an extension of the Iterator interface It has two additional methods: void set(Object o) and add(Object o)

19 set(Object o) Sets the current node to Object o. For example: Current xyz With this LinkedList, a call to set(w) would produce...

20 This! Current x w z

21 add(Object o) Adds a node immediately before what would be the result of next() Current x w z For example, a call of add(v) would produce...

22 This! Current x wzv

23 Creating an instance interator Oftentimes, an instance of Iterator of ListIterator can be created by using the iterator() or listIterator() method of a class. For LinkedList g, Iterator x = g.iterator() would produce an instance of Iterator called x that begins at g’s first node. On your AP Test, refer to the java subset to see which classes have these methods.

24 Bibliography Barron’s How to Prepare for the AP Computer Science Advanced Placement Examination Java Version http://java.sun.com/j2se/1.4.2/docs/api/java/ util/Iterator.html


Download ppt "Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class, 2005-2006."

Similar presentations


Ads by Google