Java Utility Library

Java Vector - iterator() Method



The java.util.Vector.iterator() method returns an iterator over the elements in the list in proper sequence.

Syntax

public Iterator<E> iterator()

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


Parameters

No parameter is required.

Return Value

Returns an iterator over the elements in this list in proper sequence.

Exception

NA.

Example:

In the example below, the java.util.Vector.iterator() method returns an 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 iterator
    Iterator<Integer> itr = MyVec.iterator();

    //print the content of the iterator
    System.out.println("The iterator values are: ");
    while(itr.hasNext())
      System.out.println(itr.next()); 
  }
}

The output of the above code will be:

MyVec contains: [10, 20, 30, 40]
The iterator values are: 
10
20
30
40

❮ Java.util - Vector