Java List get previous and next index using ListIterator

Description

Java List get previous and next index using ListIterator


import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class Main {
  public static void main(String[] args) {
    List<String> aList = new ArrayList<String>();

    aList.add("CSS");
    aList.add("HTML");
    aList.add("C++");
    aList.add("Java");
    aList.add("demo2s .com");

    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();/*from   w  w w.j a  va 2s  .c om*/
    System.out.println("Previous: " + listIterator.previousIndex());
    System.out.println("Next: " + listIterator.nextIndex());
  }
}



PreviousNext

Related