English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List (List)

Java Queue (queue)

Java Map collection

Java Set collection

Java input/output (I/O)

Java Reader/Writer

Java other topics

Java ListIterator Interface

In this tutorial, we will learn the Java ListIterator interface with examples.

The Java Collection Framework's ListIterator interface provides functionality to access list elements.

It is bidirectional. This means it allows us to iterate over the elements of the list in both directions.

It inherits the Iterator interface.

The List interface provides the ListIterator() method, which returns an instance of the ListIterator interface.

ListIterator methods

The ListIterator interface provides methods that can be used to perform various operations on list elements.

  • hasNext() - Returns true if there is an element in the list

  • next() - Return the next element of the list

  • nextIndex() - Return the index of the element returned by next()

  • previous() - Return the previous element of the list

  • previousIndex()- Return the index of the element returned by previous()

  • remove()- Remove the element returned by next() or previous()

  • set() - Replace the element returned by next() or previous() with the specified element

Example1: Implementation of ListIterator

In the following example, we implement the next(), nextIndex(), and hasNext() methods of the ListIterator interface in an array list.

import java.util.ArrayList;
import java.util.ListIterator;
class Main {
    public static void main(String[] args) {
        //Create an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(3);
        numbers.add(2);
        System.out.println("ArrayList: " + numbers);
        //Create an instance of ListIterator
        ListIterator<Integer> iterate = numbers.listIterator()
        //Using next() method
        int number1 = iterate.next();
        System.out.println("Next element: "); + number1);
        //Using nextIndex()
        int index1 = iterate.nextIndex();
        System.out.println("Position of the next element: "); + index1);
        //Using hasNext() method
        System.out.println("Is there a next element? "); + iterate.hasNext());
    }
}

Output Result

ArrayList:  [1, 3, 2]
Next element: 1
Position of the next element: 1
Is there a next element? true

Example2: Implementation of ListIterator

In the following example, we implement the previous() and previousIndex() methods of the ListIterator interface in an array list.

import java.util.ArrayList;
import java.util.ListIterator;
class Main {
    public static void main(String[] args) {
        //Create an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(3);
        numbers.add(2);
        System.out.println("ArrayList: " + numbers);
        //Create an instance of ListIterator
        ListIterator<Integer> iterate = numbers.listIterator()
        iterate.next();
        iterate.next();
        //Using the previous() method
        int number1 = iterate.previous();
        System.out.println("Previous element: ") + number1);
        //Using previousIndex()
        int index1 = iterate.previousIndex();
        System.out.println("The position of the previous element: ") + index1);
    }
}

Output Result

ArrayList:  [1, 3, 2]
Previous element: 3
The position of the previous element: 0

In the above example, the initial instance of the iterator is at1before. Because in1There are no elements before, so calling the previous() method will throw an exception.

Then, we used next()2This method. Now the Iterator instance will be3to2between.

Therefore, the previous() method returns3.