Java Vector listIterator(int index) Method Example

The listIterator() method of java.util.Vector class listIterator() method will return a list iterator over the elements in the list in proper sequence sameway listiterator(int index) method also returns a list iterator over the elements in the list in proper sequence, starting at the specified position in the list.

Signature

public ListIterator<E> listIterator(int index) 

This method returns a list iterator over the elements in the list in proper sequence, starting at the specified position in the list.. Returned iterator will be a fail-fast iterator. Throws java.langIndexOutOfBoundsException if the index is out of range.

Example

The following example shows the usage of java.util.Vector.listIterator(index) method.

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

public class ListIteratorMethodVectorExample 
{
    public static void main(String args[])
    {
     // create an empty Vector
        Vector vector1 = new Vector();
        
        // use add() method to add elements to the Vector
        vector1.add("Element1");
        vector1.add("Element2");
        vector1.add("Element3");
        vector1.add("Element4");
        vector1.add("Element5");
        
        //Get ListIterator object using listIterator() method
        ListIterator li = vector1.listIterator(1);
        
        //Lets iterate
        while(li.hasNext())
        {
            System.out.println(li.next());
        }
    }
}

Output

Element2
Element3
Element4
Element5

Leave a Reply

Your email address will not be published. Required fields are marked *