Java Vector iterator() Example

The iterator() method of java.util.Vector class will returns iterator over the elements in this list in proper sequence

Signature

public Iterator<E> iterator() 

This method returns iterator over the elements in this list in proper sequence. Returned iterator will be a fail-fast iterator

Example

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

import java.util.Iterator;
import java.util.Vector;

public class ContainsMethodVectorExample {
    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");
        
        //Iterating elements of the Vector
        System.out.println("Elements of the Vector");
        Iterator it = vector1.iterator();
        while(it.hasNext())
        {
            System.out.println(it.next());
        }
    }
}

Output

Elements of the Vector
Element1
Element2
Element3
Element4

Leave a Reply

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