• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to make my class iterable?

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.





 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:But I am off to cook tea now, so I shan't take any notice.

Having something nice ?
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic