How to Browse a Vector with Iterator in Java

The example below shows how to traverse through a Vector using the Iterator object. You can retrieve the Iterator object by calling the iterator().

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

public class ParcoursIterator {

public static void main(String[] args) {
Vector< String> vct = new Vector< String> ();
//add elements
vct.add("first");
vct.add("second");
vct.add("third");
vct.add("fourth");

//browse with Iterator
Iterator< String> itr = vct.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Output:

first
second
third
fourth