I am trying to do tests on a class that implements Iterable. When I go to test any method,in the itorator class, like hasNext(), next(). So when I call list.itorator().Next(); nothing happens, the node is the current node in the list not the next. but when I do ListIterator itrList = new ListIterator(list); the code inside it works. I am calling public Iterator iterator() {return new ListIterator (this);
So what am I not doing correctly/ grasping?
Thanks for any help.
public void test6() {
List<String> list = new List<String>();
list.addAfterCurrent("1");
list.addAfterCurrent("2");
list.addAfterCurrent("3");
ListIterator itrList = new ListIterator(list);
TextIO.putln(list.iterator().next()); // output is 1
TextIO.putln(list.iterator().next()); // output is 1
TextIO.putln(itrList.next()); // output is 1
TextIO.putln(itrList.next()); // now output is 2
assertEquals("1", list.getCurrent());
assertEquals(3, list.size());
}
You’re creating a fresh iterator each time you call
list.iterator(). You want to keep the same iterator and call itsnextmethod multiple times, e.g.