On Wed, Mar 19, 2008 at 1:19 PM, Thilo Goetz <[EMAIL PROTECTED]> wrote:
> <snip/>
> public Object next() {
> if (hasNext()) {
> return next;
> }
> throw new NoSuchElementException();
> }
>
> where the actual work happens in hasNext(). The user may or may not
> call hasNext(), doesn't matter. Of course in the implementation of
> hasNext(), I have to make sure to remember that I have advanced the
> iterator. Sometimes it works better that way, for internal reasons.
>
Well, I think you need something slightly more complicated in next(),
to avoid having hasNext() advance the iterator every time it is
called, which wouldn't be right. Just something like this would work
though:
public Object next() {
if (hasNext()) {
Object result = this.next;
this.next = null;
return result;
}
throw new NoSuchElementException();
}
And then in hasNext() you check if this.next != null and return true
without advancing.
> I have read the documentation again, and I think it's fine. The
> bit about distributing the processing makes perfect sense to me.
> So does the part about the calling sequence, but then it wouldn't
> occur to me to write an iterator whose hasNext() method may only be
> called once before next() is called. If you think that's necessary,
> we could add a note to indicate that hasNext() may be called more
> than once.
>
I do agree with Eddie and Burn that we could and should do a better
job in the documentation, to educate people who might not be so
familiar with how the Iterator interface defines hasNext/next.
-Adam