I am trying to do a foreach loop as followed for a linkedlist-esque class I built:
boolean contains = false;
for(Thing t : front) {
if(t.equals(something)) {
t.doSomething();
contains = true;
break;
}
}
This are the iterator methods I've implemented:
@Override
public Iterator<Thing> iterator() {
Thing current = this;
return new Iterator<Thing>() {
public boolean hasNext() {
return current.hasNext();
}
public Thing next() {
return current.next;
}
};
}
I've tried debugging this and it seems it isn't returning the next element in the linkedlist, thus causing an infinite loop when I run it and try and do a foreach loop.
Sorry if this question has been answered or there's a really stupid mistake, I searched for a while and couldn't find an answer. This is my first time writing an iterator so please be gentle. :)