wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to make my class iterable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to make my class iterable?" Watch "How to make my class iterable?" New topic
Author

How to make my class iterable?

Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102


So my question is if the next() method determines that there is no next element what do I do? Do I return null, do I throw an exception??

Thanks in advance!
Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
Looking at the API maybe I am supposed to throw UnsupportedOperationException.

Problem is when I throw this exception it propagates all the way up the stack to my main. So what do I do to get my iterator class to stop iterating when it hits the last element?


next

public Object next()
Returns the next element in the iteration.
Returns:
the next element in the iteration.
Throws:
NoSuchElementException - iteration has no more elements.





Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3431
    
  12
Theodore David Williams wrote:So what do I do to get my iterator class to stop iterating when it hits the last element?
You call the hasNext method before calling the next method.

Or you catch the exception that is thrown - which should be a NoSuchElementException not an UnsupportedOperationException.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38041
    
  22
You are making things too difficult for yourself.
Well, actually, you will have no real problems. That array, being nicely set to null, will throw a load of nice Exceptions whenever you try to use it You should delete that array, and use the same array which is already a field in the surrounding class.
You will need an int called size which should be set in the surrounding class already. You need to use that in the hasNext method
The remove() method should remove. If you are dealing with an array, you would have to reduce the rest of the array:In the else part of the next() method, you throw the NoSuchElementException.

At least I think that is what you do. Anybody finding any mistakes (especially Joanne and Rob), be sure to tell me. But I am off to cook tea now, so I shan't take any notice.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3431
    
  12
Campbell Ritchie wrote:But I am off to cook tea now, so I shan't take any notice.
Having something nice ?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Campbell Ritchie wrote:
The remove() method should remove.

Or throw an UnsupportedOperationException as the OP has done. The remove method is documented as optional (and it can be argued should have never been part of the Iterator interface to begin with).


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

I'm with Garrett on that one. You can't remove anything from arrays.

This entire discussion could be prevented by using Arrays.asList(collection).iterator()


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38041
    
  22
Joanne Neal wrote:Having something nice ?
It's Friday, and I am very conventional, so it's fish. Some salmon and some sea-bass, baked in the oven with onions tomatoes and herbs. And I would have added mushrooms if I'd remembered to buy some. And runner beans and new potatoes. The tomatoes were quite old; they had been off the plant for a good 10 minutes before they were cooked.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to make my class iterable?
 
Similar Threads
My LinkedList Iterator - Can't print first element - No use of Java API
Reader question: Tiger's for loop
Implementing Iterator problem - ConcurrentModificationException
iterator in enhanced for loop
Rant about java Iterator interface