How to get the previous index of ArrayList using ListIterator in Java

1 Answer

0 votes
package javaapplication1;

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

public class JavaApplication1 {

    public static void main(String[] args) {

        ArrayList arrList = new ArrayList();
   
        arrList.add("Java");
        arrList.add("C");
        arrList.add("C++");
        arrList.add("C#");
        arrList.add("PHP");
        arrList.add("JavaScript");

        ListIterator li = arrList.listIterator();
    
        System.out.println("Previous Index is : " + li.previousIndex());
        
        li.next();
        System.out.println("Previous Index is : " + li.previousIndex());
        
        li.next();
        li.next();
        System.out.println("Previous Index is : " + li.previousIndex());
        System.out.println("Previous Index is : " + li.previousIndex());
    }
}
 
/*
run:
 
Previous Index is : -1
Previous Index is : 0
Previous Index is : 2
Previous Index is : 2
 
*/

 



answered Oct 2, 2016 by avibootz
edited Oct 2, 2016 by avibootz
...