View Single Post
  #3 (permalink)  
Old 08-04-2007, 08:52 PM
Harb Harb is offline
Member
 
Join Date: Aug 2007
Posts: 2
Harb is on a distinguished road
Thanks for the reply, and sorry for not being very clear in my first post.

So far I have:

Code:
public class MyClass implements Iterable<AnotherClass>, Iterator<AnotherClass> { count = 0; AnotherClass[] collection; public MyClass(){ } //constructors and other stuff here ... public Iterator<AnotherClass> iterator() { return this; } public boolean hasNext() { return count < collection.size(); } public AnotherClass next() { if(count == collection.size()){ throw new NoSuchElementException(); } return collection[count]; } public void remove() { throw new UnsupportedOperationException(); }
And according to the example I've read on another site, this should work. What I don't understand though, is when/where should i reset the count variable? (I realize the class I've presented is pretty useless, I'm using it merely as an example) If I iterate through the collection once, the count variable will be incremented until it reaches the end of the collection. And if I want to iterate through it again, it seems to me I'll have a problem :P

Last edited by Harb : 08-04-2007 at 08:55 PM.
Reply With Quote