Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-04-2007, 07:07 PM
Member
 
Join Date: Aug 2007
Posts: 2
Harb is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-04-2007, 07:31 PM
Member
 
Join Date: Aug 2007
Posts: 47
henry_78 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-04-2007, 07:52 PM
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 07:55 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-04-2007, 09:18 PM
Member
 
Join Date: Aug 2007
Posts: 47
henry_78 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-05-2007, 12:58 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,191
hardwired is on a distinguished road
Seems like you would reset the count after the last element was sent out.
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]; }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-05-2007, 01:47 AM
Member
 
Join Date: Aug 2007
Posts: 47
henry_78 is on a distinguished road
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];
}
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-05-2007, 03:24 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,191
hardwired is on a distinguished road
If u do like that, u wont even know when u reach the end of the collection.

Right.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
iterator issues orchid New To Java 2 08-12-2008 02:43 PM
Iterator eva New To Java 0 01-31-2008 03:07 PM
implementing shape sidkdbl07 Java 2D 1 01-12-2008 07:42 PM
using Iterator with Vector Java Tip Java Tips 0 11-13-2007 11:52 AM
How to use an Iterator in java lenny New To Java 2 07-25-2007 08:46 PM


All times are GMT +3. The time now is 10:46 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org