The Wayback Machine - https://web.archive.org/web/20240618020218/https://www.geeksforgeeks.org/vector-listiterator-method-in-java-with-examples/
Open In App

Vector listIterator() method in Java with Examples

Last Updated : 02 Jan, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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


Output:

for
Geeks

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




// 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 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–



Previous Article
Next Article

Similar Reads

ArrayList listIterator() method in Java with Examples
listIterator() The listIterator() method of java.util.ArrayList class is used to return a list iterator over the elements in this list (in proper sequence). The returned list iterator is fail-fast. Syntax: public ListIterator listIterator() Return Value: This method returns a list iterator over the elements in this list (in proper sequence). Below
3 min read
AbstractList listIterator() Method in Java with Examples
The listIterator() method of java.util.AbstractList class is used to return a list-iterator containing the same elements as that of the AbstractList in proper and same sequence starting from a specific position or index number which is passed as a parameter to this method. Syntax: ListIterator new_list = AbstractList.listIterator(int index); Parame
2 min read
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 met
2 min read
LinkedList listIterator() Method in Java
The Java.util.LinkedList.listIterator() method is used to return a list-iterator containing the same elements as that of the LinkedList in proper and same sequence starting from a specific position or index number which is passed as a parameter to this method. Syntax: ListIterator new_list = LinkedList.listIterator(int index); Parameters: The param
1 min read
CopyOnWriteArrayList listIterator() method in Java
The listIterator() method of CopyOnWriteArrayList returns an listIterator over the elements in this list in proper sequence. The listIterator does NOT support the remove, set or add methods. Syntax: public ListIterator listIterator() Parameters: The function does not accept any parameters. Return Value: The function returns a list iterator over the
3 min read
Stack listIterator(int) method in Java with Example
The listIterator(int) method of Stack Class is used to return a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to previous would return the element with the specified
2 min read
Stack listIterator() method in Java with Example
The listIterator() method of Java.util.Stack class is used to return a list iterator over the elements in this stack (in proper sequence). The returned list iterator is fail-fast. Syntax: public ListIterator listIterator() Return Value: This method returns a list iterator over the elements in this stack (in proper sequence). Below are the examples
2 min read
Reverse an ArrayList in Java using ListIterator
Assuming you have gone through arraylist in java and know about arraylist. This post contains different examples for reversing an arraylist which are given below:1. By writing our own function(Using additional space): reverseArrayList() method in RevArrayList class contains logic for reversing an arraylist with integer objects. This method takes an
6 min read
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 l
2 min read
Difference between an Iterator and ListIterator in Java
Iterators are used in Collection framework in Java to retrieve elements one by one. It can be applied to any Collection object. By using Iterator, we can perform both read and remove operations. Iterator must be used whenever we want to enumerate elements in all Collection framework implemented interfaces like Set, List, Queue, Deque and also in al
3 min read
Practice Tags :