I also see many doing this:
public Iterator iterator() {
return this;
}
}
But that does not make it right!
This method would not be what you want!
The method iterator() is supposed to return a new iterator starting from scratch.
So one need to do something like this:
public class IterableIterator implements Iterator, Iterable {
//Constructor
IterableIterator(IterableIterator iter)
{
this.initdata = iter.initdata;
}
// methods of Iterable
public Iterator iterator() {
return new MyClass(this.somedata);
}
// methods of Iterator
public boolean hasNext() {
// ...
}
public Object next() {
// ...
}
public void remove() {
// ...
}
}
The question is: would there be any way to make an abstract class performing this?
So that to get an IterableIterator one only need to implement the two classes next() and hasNext()