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

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

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

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

Using Iterators

Trey Chandler

Burlington Williams High School

Mr. Langner’s Class, 2005-2006

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

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.

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

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

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

But,

There are some problems!!!!!!!!!

• For Example, what happens if you want to access the list in two places simultaneously?

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

The solution is...

The Iterator!

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

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

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

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

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

hasNext()

Current

• In this case hasNext() would return true

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

hasNext()

Current

• In this case hasNext() would return false

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

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

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

next()

• When next is called, it looks like this

Current

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

next()

• When next is called again, it looks like this

Current

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

next()

• When next is called again, it looks like this

Current

If next is called one more time, an exception is thrown

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

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.

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

remove()

• If a the current node is position one (meaning next() would result in pos. 2)

• and remove() is called...

Current

1 2 3

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

remove()

• The linked list would look like this

Current

2 3

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

ListIterator

• ListIterator is an extension of the Iterator interface

• It has two additional methods: void set(Object o) and add(Object o)

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

set(Object o)

• Sets the current node to Object o.

• For example:

Current

x y z

• With this LinkedList, a call to set(w) would produce...

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

This!

Current

x w z

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

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...

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

This!

Current

x w zv

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

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.

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

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