java vector demo
Vector is another data structure in Java that is very similar to ArrayList. Vector is synchronized, where as ArrayList is not. Thus ArrayList will be faster than Vector. It is recommended to use ArrayList instead of Vector, if a thread-safe implementation is not needed.
This code snippet demonstrates how to define a vector, add elements to it, modify one of the element and looping through each element in the vector.
[code language=”java”]
import java.util.Vector;
public class VectorDemo {
public static void main(String args[]) {
Vector<String> str = new Vector<String>();
str.add("aaa");
str.add("bbb");
str.add("ccc");
str.set(1, "BBB");
for(String s : str) {
System.out.println(s);
}
}
}
[/code]
Output:
[code language=”text”]
aaa
BBB
ccc
[/code]
Search within Codexpedia

Search the entire web
