Transcript Iterators

Iterators
26-Jul-16
Iterators
public interface Iterator {
boolean hasNext( );
// true if there is another element
Object next( );
// returns the next element (advances the iterator)
void remove( ); // Optional
// removes the element returned by next
}
Using an Iterator




static void printAll (Collection coll) {
Iterator iter = coll.iterator( );
while (iter.hasNext( )) {
System.out.println(iter.next( ) );
}
}
hasNext() just checks if there are any more elements
next() returns the next element and advances in the
collection
Note that this code is polymorphic—it will work for
any collection
ConcurrentModificationException


static void printAll (Collection coll) {
Iterator iter = coll.iterator( );
// When you create an iterator, a “fingerprint”
// of the collection (list or array) is taken
while (iter.hasNext( )) {
System.out.println(iter.next( ) );
// Both hasNext and next check to make sure
// the collection hasn’t been altered, and will
// throw a ConcurrentModificationException
// if it has
}
}
This means you cannot add or remove elements from the collection within the
loop, or any method called from within the loop, or from some other Thread
that has nothing to do with the loop
4
New for statement



The syntax of the new statement is
for(type var : array) {...}
or for(type var : collection) {...}
Example:
for(float x : myRealArray) {
myRealSum += x;
}
For a collection class that has an Iterator, instead of
for (Iterator iter = c.iterator(); iter.hasNext(); )
((TimerTask) iter.next()).cancel();
you can now say
for (TimerTask task : c)
task.cancel();

Note that this for loop is implemented with an Iterator!
5
Solutions
1. Don’t use an iterator!



There are other kinds of loops you can use
You have to be careful, because the array or list might grow
or shrink while you are stepping through it
So long as the program doesn’t crash, the result will
probably be a trivial (unnoticeable) glitch in the drawing
2. Synchronize all references to the collection

“All references” means all references
6
The synchronized statement



Synchronization is a way of providing exclusive access to data
You can synchronize on any Object, of any type
If two Threads try to execute code that is synchronized on the
same object, only one of them can execute at a time; the other has
to wait



synchronized (someObject) { /* some code */ }
This works whether the two Threads try to execute the same block of code,
or different blocks of code that synchronize on the same object
Often, the object you synchronize on bears some relationship to
the data you wish to manipulate, but this is not at all necessary
7
synchronized methods

Instance methods can be synchronized:


This is equivalent to


synchronized public void myMethod( /* arguments */) {
/* some statements */
}
public void myMethod( /* arguments */) {
synchronized(this) {
/* some statements */
}
}
Static methods can also be synchronized

They are synchronized on the class object (a built-in object that represents
the class)
8
The End