I'm a newbie in java,
I want to traverse the elements in a Vector
. I'm getting the same result when I use Iterator
/ Enumeration
interfaces.
As Vector
is slow because it is synchronized. Does it enhance the performance in any aspect if I use Iterator
/ Enumeration
.
Here's what I've tried
import java.util.*;
class vectorClass{
public static void main(String args[]){
Vector<String> vector = new Vector<String>();
vector.add("This is a vector Example\n");
vector.add("This is a next line!");
Enumeration en = vector.elements();
while(en.hasMoreElements()){
System.out.print(en.nextElement());
}
System.out.println();
Iterator en1 = vector.iterator();
while(en1.hasNext()){
System.out.print(en1.next());
}
}
}
O/P:
This is a vector Example
This is a next line!
This is a vector Example
This is a next line!