ListIterator previous method not working
Asked Answered
A

6

7
package wrap;
import java.util.*;
public class ArrayListDemo {

    public static void main(String [] args){
        ArrayList<String> a=new ArrayList<String>();
        a.add("B");
        a.add("C");
        a.add("D");
        ListIterator<String> i=a.listIterator();
        while(i.hasPrevious()){
            System.out.println(i.previous());
        }
    }

}

The program works fine for hasNext() and next() methods but for hasPrevious() and previous() it displays a message as below::

<terminated> ArrayListDemo [Java Application] C:\Program Files (x86)\Java\jre7\bin\javaw.exe (28-Oct-2013 3:20:35 PM)
Atween answered 28/10, 2013 at 9:56 Comment(0)
P
19

From the doc :

public ListIterator<E> listIterator()

Returns a list iterator over the elements in this list (in proper sequence).

and

boolean hasPrevious()

Returns true if this list iterator has more elements when traversing the list in the reverse direction.

Because the iterator is in the first position, hasPrevious() will return false and hence the while loop is not executed.

 a's elements

    "B"  "C"  "D"
     ^
     |

Iterator is in first position so there is no previous element

If you do :

    ListIterator<String> i=a.listIterator(); <- in first position
    i.next(); <- move the iterator to the second position
    while(i.hasPrevious()){
        System.out.println(i.previous());
    }

It will print "B" because you're in the following situation :


a's elements
        "B"  "C"  "D"
              ^
              |
    Iterator is in second position so the previous element is "B"

You could also use the method listIterator(int index). It allows you to place the iterator at the position defined by index.

If you do :

ListIterator<String> i=a.listIterator(a.size());

It will print

D
C
B
Proceleusmatic answered 28/10, 2013 at 9:57 Comment(0)
F
4

Since you get the default ListIterator for the list, it starts with the first element, which is why hasPrevious() returns false and the while loop is exited. If you want to traverse the list in the reverse order, get the ListIterator from the last index and traverse backwards using the hasPrevious() and previous() methods.

ListIterator<String> i = a.listIterator(a.size()); // Get the list iterator from the last index
while (i.hasPrevious()) {
    System.out.println(i.previous());
}
Floorboard answered 28/10, 2013 at 10:1 Comment(0)
F
2
ListIterator<String> i=a.listIterator();

initially iterator i will point to index of 0

You are at index 0 so there is no previous element.

Fy answered 28/10, 2013 at 9:58 Comment(0)
U
0

It will start at the front of the list and thus nothing is before that point. If you want to use these methods, use a ListIterator.

Docs.

Unloosen answered 28/10, 2013 at 9:57 Comment(0)
D
0

You gave to reach to the end of the list or anywhere in between to traverse back. There is no previous element to element at index 0.

Eg

System.out.println("Elements in forward directiton");
        while(i.hasNext()){
            System.out.println(i.next());
        }
System.out.println("Elements in backward directiton");
        while(i.hasPrevious()){
            System.out.println(i.previous());
        }
Dispirited answered 28/10, 2013 at 9:59 Comment(0)
E
0

As said by others, You pointer is still pointing to first element in List so -1 is not possible.

List of size n will have 1,2 ....n-1 as index.

n-1 is not -1.

THis is the reason why hasPrevious and previous are not working for you.

Cheers.

Errol answered 23/9, 2019 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.