Java Collection Tutorial - Java ListIterator.previousIndex()








Syntax

ListIterator.previousIndex() has the following syntax.

int previousIndex()

Example

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

import java.util.ArrayList;
import java.util.ListIterator;
/*from   w w  w  .  j a v a  2s . co  m*/
public class Main {
  public static void main(String[] args) {
    ArrayList<String> aList = new ArrayList<String>();

    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");

    ListIterator<String> listIterator = aList.listIterator();
    System.out.println("Previous: " + listIterator.previousIndex());
    System.out.println("Next: " + listIterator.nextIndex());

    // advance current position by one using next method
    listIterator.next();
    System.out.println("Previous: " + listIterator.previousIndex());
    System.out.println("Next: " + listIterator.nextIndex());
  }
}

The code above generates the following result.