I have the following piece of code:
LinkedList<String> list = new LinkedList<String>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
list.add("6");
ListIterator<String> iterator = list.listIterator();
String str = iterator.next();
System.out.println(str);
str = iterator.next();
System.out.println(str);
str = iterator.next();
System.out.println(str);
System.out.println("switch direction");
str = iterator.previous();
System.out.println(str);
str = iterator.previous();
System.out.println(str);
the output is this:
1
2
3
switch direction
3
2
I would expect to print 2 on first iterator.previous() call, but is not. It gaves me the same element, it is like this function is doing nothing on it's first call.
the same thing happens if I start the iteration from the last to the first and I change direction in the middle back to the last.
Is there a design reason for that or could it be just a bug in Java's list iterator?
I am using:
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)