A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/vector-listiterator-method-in-java-with-examples/ below:

Vector listIterator() method in Java with Examples

Vector listIterator() method in Java with Examples

Last Updated : 11 Jul, 2025

java.util.Vector.listIterator()

This method returns a list iterator over the elements of a

Vector

object in proper sequence. It is bidirectional, so both forward and backward traversal is possible, using

next()

and

previous()

respectively. The iterator thus returned is

fail-fast

. This means that structurally modifying the vector after the iterator is created, in any way except through the

iterator's own

remove or add methods (using

Vector

.add(), for example), will cause iterator to throw

ConcurrentModificationException

.

Syntax:
public ListIterator listIterator()
Parameters:

This method accepts no input arguments.

Return Value:

This method returns a

ListIterator

object which can be used to traverse the Vector object.

Example 1:

To demonstrate forward and backward traversal using listIterator().

Java
// Java code to illustrate listIterator()

import java.util.Vector;
import java.util.ListIterator;

public class GFG {
    public static void main(String[] args)
    {
        // Declare empty vector
        Vector<String> vt = new Vector<String>();

        vt.add("Geeks");
        vt.add("for");
        vt.add("Geeks");
        vt.add("2019");
        vt.add("AComputerSciencePortalForGeeks");

        // Declare list iterator
        ListIterator listItr = vt.listIterator();

        // Forward iterations
        System.out.println("Forward Traversal:");
        while (listItr.hasNext()) {
            System.out.println(listItr.next());
        }

        // Backward iterations
        System.out.println("\nBackward Traversal:");
        while (listItr.hasPrevious()) {
            System.out.println(listItr.previous());
        }
    }
}
Output:
Forward Traversal:
Geeks
for
Geeks
2019
AComputerSciencePortalForGeeks

Backward Traversal:
AComputerSciencePortalForGeeks
2019
Geeks
for
Geeks
java.util.Vector.listIterator(int index)

This method is used to return a list iterator by specifying starting index. Also bidirectional and fail-fast.

Syntax:
public ListIterator listIterator(int index)
Parameters:

The parameter index is an integer type value that specifies the first element to be returned from the list iterator (by a call to

next()

).

Return Value:

This method returns a

ListIterator

object which can be used to traverse the Vector object.

Exception:

This method throws

IndexOutOfBoundsException

, if the index is out of range (index < 0 or index > size())

Example 2:

To demonstrate listIterator(int index).

Java
// Java code to illustrate listIterator(int index)

import java.util.Vector;
import java.util.ListIterator;

public class GFG {
    public static void main(String[] args)
    {
        // Declare empty vector
        Vector<String> vt = new Vector<String>();

        vt.add("Geeks");
        vt.add("for");
        vt.add("Geeks");

        // Declare list iterator
        ListIterator listItr = vt.listIterator(1);
        // traversal
        while (listItr.hasNext()) {
            System.out.println(listItr.next());
        }
    }
}
Example 3:

To demonstrate

IndexOutOfBoundsException

thrown by listIterator(int index).

Java
// Java code to illustrate IndexOutOfBoundsException
// thrown by listIterator(int index)

import java.util.Vector;
import java.util.ListIterator;

public class GFG {
    public static void main(String[] args)
    {
        // Declare empty vector
        Vector<String> vt
            = new Vector<String>();

        vt.add("Geeks");
        vt.add("for");
        vt.add("Geeks");

        // Declare list iterator at starting
        // index greater than vector size
        try {
            ListIterator listItr
                = vt.listIterator(5);
        }
        catch (IndexOutOfBoundsException e) {
            // Exception handling
            System.out.println("Exception: " + e);
        }
    }
}
Output:
Exception: java.lang.IndexOutOfBoundsException: Index: 5
Example 4:

To demonstrate

ConcurrentModificationException

thrown by ListIterator object when Vector object is modified after creating list iterator to it.

Java
// Java code to illustrate ConcurrentModificationException
// thrown by ListIterator object

import java.util.ConcurrentModificationException;
import java.util.Vector;
import java.util.ListIterator;

public class GFG {
    public static void main(String[] args)
    {
        // Declare empty vector
        Vector<String> vt = new Vector<String>();

        vt.add("Geeks");
        vt.add("for");

        // Declare list iterator
        ListIterator listItr = vt.listIterator();

        // modify vector after creating list iterator
        vt.add("Geeks");

        try {
            // Exception thrown here
            System.out.println(listItr.next());
        }
        catch (ConcurrentModificationException e) {
            // Exception handling
            System.out.println("Exception: " + e);
        }
    }
}
Output:
Exception: java.util.ConcurrentModificationException
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html#listIterator--

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4