I have a university assignment that requires me to implement an inner class that implements the Iterator interface. An iterator is working on a simply connected superclass.
Currently, my inner class is as follows:
private class ListIterator implements Iterator<V>{ Node temp; boolean nextCalled = false; ListIterator(Node fo){ this.temp = fo; } @Override public boolean hasNext() { if(temp != null){ return true; } return false; } @Override public V next() { nextCalled = true; return temp.getReprValue(); } @Override public void remove() { if(nextCalled && hasNext()){ nextCalled = false; removeElement(temp.getReprKey()); temp = temp.getNext(); } } }
Now my problem is that the hasNext () method returns true even if the list is actually empty. Everything else seems to work. I probably forgot a logical flaw somewhere, but I can't find it myself.
java iterator linked-list iterable
Henrik Hillestad LΓΈvold
source share