Java ListIterator.hasPrevious()

Syntax

ListIterator.hasPrevious() has the following syntax.

boolean hasPrevious()

Example

In the following code shows how to use ListIterator.hasPrevious() method.


import java.util.ArrayList;
import java.util.ListIterator;
/*ww  w  . ja  va2 s.co  m*/
public class Main {

  public static void main(String[] args) {

    ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    arrayList.add("4");
    arrayList.add("5");

    ListIterator itr = arrayList.listIterator();
    System.out.println("in forward direction");
    while (itr.hasNext()) {
      System.out.println(itr.next());
    }

    System.out.println("in backward direction");
    while (itr.hasPrevious()) {
      System.out.println(itr.previous());
    }
  }
}

The code above generates the following result.