Searching...
Saturday, 6 October 2012

Java Collections: Vector Example

04:28
          Same as the class ArrayList
          The main difference is that the methods of Vector are synchronized
image

Example

package mycollectiontest;

import java.util.Enumeration;
import java.util.Vector;

public class CollectionExample {

public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
// Adding objects
vector.add("Java");
vector.add("PHP");
vector.add(".Net");
vector.add("Android");
vector.add("Windows");
vector.add("Linux");
System.out.println("Fetching elements");
System.out.println(vector.get(0));
System.out.println(vector.get(1));
System.out.println(vector.get(2));
System.out.println(vector.get(3));
System.out.println(vector.get(4));
System.out.println(vector.get(5));
System.out.println("Size of vectoror : " + vector.size());
Enumeration vectorElementsEnum = vector.elements();
while (vectorElementsEnum.hasMoreElements())
System.out.println(vectorElementsEnum.nextElement());
}

}

Output

Fetching elements
Java
PHP
.Net
Android
Windows
Linux
Size of vectoror : 6
Java
PHP
.Net
Android
Windows
Linux

0 comments: