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?
public Object next()
Returns the next element in the iteration.
the next element in the iteration.
NoSuchElementException - iteration has no more elements.
You call the hasNext method before calling the next method.
Theodore David Williams wrote:So what do I do to get my iterator class to stop iterating when it hits the last element?
Or you catch the exception that is thrown - which should be a NoSuchElementException not an UnsupportedOperationException.
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.
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.
Joanne Neal wrote:Having something nice ?