java tutorial - ListIterator in Java - java programming - learn java - java basics - java for beginners



 How to use list iterator

Learn java - java tutorial - How to use list iterator - java examples - java programs

Java ListIterator :

  • Java ListIterator is much the same as an Iterator that is used to iterate elements one-by-one from a List implemented object.
  • It is offered since Java 1.2.
  • The ListIterator extends Iterator interface.
  • It is useful only for List implemented classes.
  • Unlike Iterator, It supports all the four operations: CRUD (CREATE, READ, UPDATE and DELETE).
  • Unlike Iterator, It also supports both Forward Direction and Backward Direction iterations.
  • It is a Bi-directional Iterator.
  • It has no current element; its cursor position always lies between the element that will be returned by a call to previous() and the element that will be returned by a call to next().

Syntax:

listIterator(int index)

Parameters:

Name Description Type
index index of the first element to be returned from the list iterator (by a call to next) int

Return Value :

  • A list iterator over the elements in this list (in correct sequence), initial at the specified position in the list

Sample Code:

import java.util.*;
public class  kaashiv_infotech
{
     public static void main(String[] args) 
     {
        ArrayList<String> values = new ArrayList<String>();
        values.add("arjun");
        values.add("tiger");
        values.add("rohny");
        values.add("jimy");
        values.add("praveen");
        ListIterator<String> rol = values.listIterator();
        while (rol.hasNext())
            System.out.println(rol.next());
        System.out.println("");
        System.out.println("");
        while (rol.hasPrevious())
            System.out.println(rol.previous());
    }
}

Output:

arjun
tiger
rohny
jimy
praveen


praveen
jimy
rohny
tiger
arjun

Related Searches to ListIterator in Java