To iterate Vector via Iterator in Java
Iterator<Integer> vItr = v.iterator(); System.out.println("\nElements in vector:"); while(vItr.hasNext()) System.out.print(vItr.next() + " ");
Full source
import java.util.Iterator; import java.util.Vector; public class Main { public static void main(String args[]) { // initial size is 3, increment is 2 Vector<Integer> v = new Vector<Integer>(3, 2); System.out.println("Initial size: " + v.size()); System.out.println("Initial capacity: " + v.capacity()); v.addElement(1);/*from www . j a va 2 s . c om*/ v.addElement(2); v.addElement(3); v.addElement(4); Iterator<Integer> vItr = v.iterator(); System.out.println("\nElements in vector:"); while(vItr.hasNext()) System.out.print(vItr.next() + " "); System.out.println(); } }