java ListIterator does not return expected items

890 views Asked by At

I have the following code showing how ListIterator works but it appears the items returned by the iterator are not what I expected.

import java.util.*;
public class IteratorExample {

 public static void main(String args[]) {

  ArrayList al = new ArrayList();

  al.add("A");
  al.add("B");
  al.add("C");

  ListIterator litr = al.listIterator();

  System.out.print(litr.next());  // expect A
  System.out.print(litr.next());  // expect B
  System.out.print(litr.next());  // expect C
  System.out.print(litr.previous());  // expect B
  System.out.print(litr.previous());  // expect A
  System.out.print(litr.next());      // expect B
  System.out.print(litr.previous());  // expect A
}}

I expected to see "ABCBABA", but the sample program gives me "ABCCBBB". Can anyone explain how iterator works? What should I do if I want to end up with the result "ABCBABA" by just using the iterator?

3

There are 3 answers

0
VHS On BEST ANSWER

Here's another simple way to understand this behavior.

next() --> Return the next element and advance the cursor by one element so that now the cursor points to next to next element.

previous() --> Return the previous element and move the cursor backward by one element so that the cursor points to the previous element.

Just after your list is created, the iterator position is something like following:

ListIterator litr = al.listIterator();

   A   B   C
 ^
  System.out.print(litr.next());  // print A and move to next

   A   B   C
     ^
  System.out.print(litr.next());  // print B and move to next

   A   B   C
         ^
  System.out.print(litr.next());  // print C and move to next

   A   B   C
             ^
  System.out.print(litr.previous());  // print previous which is C and move backward

   A   B   C
         ^
  System.out.print(litr.previous());  // print previous which is B and move backward

   A   B   C
     ^
  System.out.print(litr.next());      // print B and move to next

   A   B   C
         ^
  System.out.print(litr.previous());  // print previous which is B and move backward

   A   B   C
     ^

So the output is rightly "ABCCBBB"

0
Paweł Albecki On

A ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next(). An iterator for a list of length n has n+1 possible cursor positions, as illustrated by the carets (^) below:

              Element(0)   Element(1)   Element(2)   ... Element(n-1)
positions:  ^            ^            ^            ^                  ^

ListIterator documentation

2
Ninad Shaha On

If you read java doc for listIterator.previous() method then below is what has been mentioned which answers your question:

"Returns the previous element in the list. This method may be called repeatedly to iterate through the list backwards, or intermixed with calls to next to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)"