Friday, November 21, 2025
HomeLanguagesJavaJava AbstractSequentialList | ListIterator()

Java AbstractSequentialList | ListIterator()

AbstractSequentialList listIterator(): method in Java is used to get a listIterator over this list. It returns a list iterator over the elements in this list (in proper sequence).

Syntax:

public abstract ListIterator listIterator(int index)

Parameters: This method takes a parameter index which is the index of first element to be returned from the list iterator (by a call to the next method)

Returns: This method returns a list iterator over the elements in this list (in proper sequence).

Exceptions: This method throws IndexOutOfBoundsException, if the index is out of range (index size())

Below is the code to illustrate ListIterator():

Program 1:




// Java program to demonstrate
// add() method
  
import java.util.*;
  
public class GfG {
  
    public static void main(String[] args)
    {
        // Creating an instance of the AbstractSequentialList
        AbstractSequentialList<Integer> absl = new LinkedList<>();
  
        // adding elements to absl
        absl.add(5);
        absl.add(6);
        absl.add(7);
        absl.add(2, 8);
        absl.add(2, 7);
        absl.add(1, 9);
        absl.add(4, 10);
  
        // Getting ListIterator
        ListIterator<Integer> Itr = absl.listIterator(2);
  
        // Traversing elements
        while (Itr.hasNext()) {
            System.out.print(Itr.next() + " ");
        }
    }
}


Output:

6 7 10 8 7

Program 2: To demonstrate IndexOutOfBoundException




// Java code to show IndexOutofBoundException
  
import java.util.*;
  
public class GfG {
  
    public static void main(String[] args)
    {
  
        // Creating an instance of the AbstractSequentialList
        AbstractSequentialList<Integer> absl = new LinkedList<>();
  
        // adding elements to absl
        absl.add(5);
        absl.add(6);
        absl.add(7);
        absl.add(2, 8);
        absl.add(2, 7);
        absl.add(1, 9);
        absl.add(4, 10);
  
        // Printing the list
        System.out.println(absl);
  
        try {
            // showing IndexOutOfBoundsException
            // Getting ListIterator
            ListIterator<Integer> Itr = absl.listIterator(15);
  
            // Traversing elements
            while (Itr.hasNext()) {
                System.out.print(Itr.next() + " ");
            }
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}


Output:

[5, 9, 6, 7, 10, 8, 7]
Exception: java.lang.IndexOutOfBoundsException: Index: 15, Size: 7
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32405 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6781 POSTS0 COMMENTS
Nicole Veronica
11928 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11996 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7166 POSTS0 COMMENTS
Thapelo Manthata
6862 POSTS0 COMMENTS
Umr Jansen
6847 POSTS0 COMMENTS