Java Utility Library

Java Vector - listIterator() Method



The java.util.Vector.listIterator() method returns a list iterator over the elements in the list (in proper sequence). It is bidirectional, so both forward and backward traversal is possible, using next() and previous() respectively.

Syntax

public ListIterator<E> listIterator()

Here, E is the type of element maintained by the container.


Parameters

No parameter is required.

Return Value

Returns a list iterator over the elements in the list (in proper sequence).

Exception

NA.

Example:

In the example below, the java.util.Vector.listIterator() method returns a list iterator over the elements of the given vector.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a vector
    Vector<Integer> MyVec = new Vector<Integer>();

    //populating vector
    MyVec.add(10);
    MyVec.add(20);
    MyVec.add(30);
    MyVec.add(40);

    //print the content of the vector
    System.out.println("MyVec contains: " + MyVec);

    //creating an listIterator
    ListIterator itr = MyVec.listIterator();

    //Forward Traversal
    System.out.println("Forward Traversal: ");
    while(itr.hasNext())
      System.out.println(itr.next()); 

    //Backward Traversal
    System.out.println("Backward Traversal: ");
    while(itr.hasPrevious())
      System.out.println(itr.previous()); 
  }
}

The output of the above code will be:

MyVec contains: [10, 20, 30, 40]
Forward Traversal: 
10
20
30
40
Backward Traversal: 
40
30
20
10

❮ Java.util - Vector