Get Previous and next index using ListIterator - Java Collection Framework

Java examples for Collection Framework:ListIterator

Description

Get Previous and next index using ListIterator

Demo Code


import java.util.ListIterator;
import java.util.ArrayList;
 
public class Main {
 
  public static void main(String[] args) {
    ArrayList aList = new ArrayList();
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");
   /* w  ww  .  java2  s .c  om*/
    ListIterator listIterator = aList.listIterator();
 
    System.out.println("Previous Index is : " + listIterator.previousIndex());  
    System.out.println("Next Index is : " + listIterator.nextIndex());
   
    listIterator.next();
   
    System.out.println("After increasing current position by one element : ");
    System.out.println("Previous Index is : " + listIterator.previousIndex());  
    System.out.println("Next Index is : " + listIterator.nextIndex());
  }
}

Result


Related Tutorials