Originally Posted by
anahuacv
@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() {
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 :-).