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.

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);
		}
	}
}

Output:

aaa
BBB
ccc

Search within Codexpedia

Custom Search

Search the entire web

Custom Search