Vector listIterator() method in Java with Examples

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().

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
        }
    }
}

chevron_right


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).

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
        }
    }
}

chevron_right


Output:



for
Geeks

Example 3: To demonstrate IndexOutOfBoundsException thrown by listIterator(int index).

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Exception: java.util.ConcurrentModificationException

Reference:
https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html#listIterator–



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.