List listIterator() Method in Java with Examples

This method returns a list iterator over the elements in the mentioned list (in proper sequence), starting at the specified position in the list.

Syntax:

ListIterator listIterator(int index)

Parameters: This method has only argument, i.e, index – index of the first element to be returned from the list iterator (by a call to next).



Returns: This method returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

Exception: This method throws an exception IndexOutOfBoundsException – if the index is out of range (index size())

Below programs show the implementation of this method.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// listIterator() method
// for List interface
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // Creating object of List<Integer>
            List<Integer> arrlist = new ArrayList<>();
  
            // adding element to arrlist
            arrlist.add(1);
            arrlist.add(3);
            arrlist.add(6);
            arrlist.add(9);
  
            // print arrlist
            System.out.println("ArrayList: "
                               + arrlist);
  
            // Creating object of ListIterator<String>
            // using listIterator() method
            ListIterator<Integer>
                iterator = arrlist.listIterator();
  
            // Printing the iterated value
            System.out.println("\nUsing ListIterator:\n");
            while (iterator.hasNext()) {
                System.out.println("Value is : "
                                   + iterator.next());
            }
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

ArrayList: [1, 3, 6, 9]

Using ListIterator:

Value is : 1
Value is : 3
Value is : 6
Value is : 9

Program 2: Below is the code to show implementation of list.subList() using Linkedlist.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// listIterator() method
// for List interface
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // Creating object of List<Integer>
            List<String> arrlist = new ArrayList<String>();
  
            // adding element to arrlist
            arrlist.add("A");
            arrlist.add("B");
            arrlist.add("C");
            arrlist.add("D");
  
            // print arrlist
            System.out.println("ArrayList: "
                               + arrlist);
  
            // Creating object of ListIterator<String>
            // using listIterator() method
            ListIterator<String>
                iterator = arrlist.listIterator();
  
            // Printing the iterated value
            System.out.println("\nUsing ListIterator:\n");
            while (iterator.hasNext()) {
                System.out.println("Value is : "
                                   + iterator.next());
            }
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

ArrayList: [A, B, C, D]

Using ListIterator:

Value is : A
Value is : B
Value is : C
Value is : D

Reference:
Oracle Docs



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.