Thursday, September 24, 2009

Java - how to create and use a Vector

import java.util.*;
public class Main {
 
    public static void main(String[] args) {
        Vector v = new Vector();
        // The Vector class is almost identical to the 
        //  ArrayList class.  The main difference is that 
        //  a Vector is thread safe. 
        v.add("number one is a String");
        v.add(22); // number two is an Integer 
 
        for (int i=0; i<10; i++){
            // add ten integers 
            v.add(i);
        }
 
        // iterate through the vector with a foreach 
        for (Object o:v){
            System.out.println(o);
        }
        // as with ArrayLists you'll notice that 
        //  Vectors are not type specific.  You can 
        //  put in whatever you want 
    }
}
 
 

No comments:

Post a Comment