Java Reference
In-Depth Information
E next() throws NoSuchElementException;
void remove() throws UnsupportedOperationException,
IllegalStateException;
}
The hasNext method returns true if next has more elements to return.
The remove method removes from the collection the last element re-
turned by next . But remove is optional, which means an implementation is
allowed to throw UnsupportedOperationException . If remove is invoked be-
fore next , or is invoked multiple times after a single call to next , then
IllegalStateException is thrown. NoSuchElementException also part of the
java.util packageis thrown if next is invoked when there are no more
elements. See " Iteration " on page 571 for more details.
Here is a simple method that returns an Iterator to walk through an ar-
ray of Object instances:
public static Iterator<Object>
walkThrough(final Object[] objs) {
class Iter implements Iterator<Object> {
private int pos = 0;
public boolean hasNext() {
return (pos < objs.length);
}
public Object next() throws NoSuchElementException {
if (pos >= objs.length)
throw new NoSuchElementException();
return objs[pos++];
}
public void remove() {
throw new UnsupportedOperationException();
}
}
return new Iter();
}
 
Search WWH ::




Custom Search