Let examine the myiterator class that follows
Looping Constructs
Implementing the Iterator interface
As mentioned earlier, any class that implements the Iterable interface can be used with the for-each statement. To illustrate this we will create two classes:
import java.util.Iterator;
public class MyIterator implements Iterator<Integer> { private int value;
private final int size;
[ 164 ]
Chapter 5
@Override
public Iterator<Integer> iterator() {
return iterator;
}
}We can test these classes with the following code sequence:
|
||
---|---|---|
statement provides a much more convenient and simple technique. |
[ 165 ]