Class java.util.Vector used as array


  • Often used instead of arrays cause of unrestricted length. This approach is supported by using indizes:
    Vector myarray = new Vector();
    myarray.setSize(4);
    String test = "test";
    mylist.setElementAt( new String("Name") ,0);
    mylist.setElementAt( test, 1);
    mylist.setElementAt( new AnyObject(), 2);
    mylist.setElementAt( new AnyOtherObject(), 3);
    
    for (int i=0; i < mylist.size(); i++)
    	System.out.println("Element " + i + ":"
    		+ mylist.elementAt(i) );
    
  • Primitive datatypes can not be stored directly. Use shell classes from the java.lang package:Double, Integer, etc.
  • Optimizes storage management by using capacity and capacityIncrement to reduce reallocation.

Java Tutorial - Standard Classes Jens Trapp, 1997