Results 1 to 14 of 14
Thread: Implementing Iterator
- 08-04-2007, 06:07 PM #1
Member
- Join Date
- Aug 2007
- Posts
- 2
- Rep Power
- 0
Implementing Iterator
Hello.
I'm trying to make a class that I want to be able to use in a "foreach" statement. My question is, how does the object know when to start over in a new iteration? (i.e. next time I want to iterate through the collection)
I'm using a counter with the next() method to keep track of which object it is currently on, but I'm unsure of which method is supposed to reset the counter to start over from the beginning.
- 08-04-2007, 06:31 PM #2
Member
- Join Date
- Aug 2007
- Posts
- 47
- Rep Power
- 0
Iterator
Hi,
First of all, Iterator is a java interface and it has methods to iterate on elements of collections.
Un can create your own iterator implementing that interface:
The methods of the interface are:
hasNext() : To know if there other elements left in the collection.
next() : Returns the next element.
remove() : To remove the current element from the collection(better not use that metthod).
U can read more about the Iterator in the java doc.
Bye.
- 08-04-2007, 06:52 PM #3
Member
- Join Date
- Aug 2007
- Posts
- 2
- Rep Power
- 0
Thanks for the reply, and sorry for not being very clear in my first post.
So far I have:
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 :PJava 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(); }Last edited by Harb; 08-04-2007 at 06:55 PM.
- 08-04-2007, 08:18 PM #4
Member
- Join Date
- Aug 2007
- Posts
- 47
- Rep Power
- 0
Hi,
In this implementation u have to increment 'count' when u invoke the next() method, this will help u to know when your collection is finished.
As u can see in your code, the hasNext() method verifies that
count < collection.size();
To iterate another time, u have to create another itaerator...or change your class adding more methods.
Bye.
- 08-04-2007, 11:58 PM #5
Seems like you would reset the count after the last element was sent out.
Java Code:public AnotherClass next() { if(count == collection.size()){ throw new NoSuchElementException(); } if(count == collection.size()-1) { // last element being sent out. count = 0; return collection[collection.size()-1]; } return collection[count]; }
- 08-05-2007, 12:47 AM #6
Member
- Join Date
- Aug 2007
- Posts
- 47
- Rep Power
- 0
If u do like that, u wont even know when u reach the end of the collection.
When a n iterator reach the end of a collection it stops..it dosen't go back to the first element.
U should change only the next() method like that:
public AnotherClass next() {
if(count == collection.size() ){
throw new NoSuchElementException();
}
count++;
return collection[count];
}
- 08-05-2007, 02:24 AM #7
If u do like that, u wont even know when u reach the end of the collection.
Right.
- 11-21-2009, 01:23 PM #8
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
@henry_78 Your solution is not ok, because you will never return the element in position 0, i.e. you are incrementing the counter before returning the element.
public AnotherClass next() {
if(count == collection.size() ){
throw new NoSuchElementException();
}
count++;
return collection[count];
}
Solution:
public AnotherClass next() {
if(count == collection.size() ){
throw new NoSuchElementException();
}
return collection[count++];
}
When you receive the NoSuchElementException you might want to call a reset method. i.e.
public boolean reset() {
return count 0;
}
The idea of creating a new iteration sucks, 'cause Java is slow as a turtle (its advantage: easy to use), so try to do things as efficient as possible no matter is not the java way :-).
- 11-21-2009, 03:16 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Make your Iterator implementation another (inner) class with its own count variable; as it is now you can only have one iterator at any time; (try to get two Iterators and you see what I mean).
kind regards,
Jos
-
This thread's a bit long in the tooth, but it's an interesting discussion nevertheless.
- 11-21-2009, 04:56 PM #11
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
- 11-21-2009, 06:21 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Resetting that count variable doesn't help you one bit when you have more than one Iterator instantiated; they'll share that variable and you don't want that. Build a separate class for the Iterator that has its own count variable. It is a bad design smell if a class is more than one thing, here: an Iterable and an Iterator. Those two things are two opposites, don't cram them in one single class.
kind regards,
Jos
- 11-21-2009, 06:30 PM #13
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
It depends of your needs, neither solution is bad applied to some scenarios, as you said iterable and iterator.
- 11-21-2009, 07:25 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
iterator issues
By orchid in forum New To JavaReplies: 2Last Post: 08-12-2008, 01:43 PM -
Iterator
By eva in forum New To JavaReplies: 0Last Post: 01-31-2008, 02:07 PM -
implementing shape
By sidkdbl07 in forum Java 2DReplies: 1Last Post: 01-12-2008, 06:42 PM -
using Iterator with Vector
By Java Tip in forum Java TipReplies: 0Last Post: 11-13-2007, 10:52 AM -
How to use an Iterator in java
By lenny in forum New To JavaReplies: 2Last Post: 07-25-2007, 07:46 PM


LinkBack URL
About LinkBacks


Bookmarks