Vector class

 

See  Collection interface  and  List interface  for descriptions and examples of Vector's inherited methods. 

 

 

Vector is part of List. It extends AbstractList which extends AbstractCollection. However those two parent classes don't yield any new methods for you to use, so they're not discussed on this site.

 

■ Since Java v1.2, you are generally directed to use ArrayList instead of Vector. But Vector still works fine. Plus Vector is synchronized, whereas ArrayList isn't.  Use Vector if synchronization is important to you.  Also Vector has methods for operating on the middle and ends of your list. With ArrayList, you don't have these, and you have to go to a TreeList to get them.  So it's no wonder that Vector remains popular!

 

  A Vector holds objects in the order in which you add them.

 

Vector always returns Objects, so you must cast returned textual elements, for instance to String, where v is your Vector. i.e.

 

String s = (String) v.get( 0 );

 

  You can use either iterators or enumerations with Vector. If you use enumerations, the Vector can be modified at any time, by anyone, without consequence.  If you use iterators and the Vector is modified not using the iterator's .add(...) and .remove(...) methods, you will get a ConcurrentModificationException.

 

■ A Vector grows automatically - although somewhat unpredictably - to accommodate new elements. 

 

Vector constructors:

 

Vector v = new Vector( );                creates an empty Vector of element capacity 10

Vector v = new Vector( Collection ); creates a Vector of the specified collection's elements

Vector v = new Vector( int );            creates an empty Vector of the capacity specified

Vector v = new Vector( int, int );       creates an empty Vector of the capacity specified,

                                                with the capacity increment specified

 

■ Shown below are methods added by Vector to the methods already specified for it by the Collection and List interfaces:

 

 

void .addElement( Obj )   method

 

■ This method adds an object to the end of the Vector.  It behaves identically to the .add(...) method of List.  see .add(...) under List interface

 

 

int .capacity( )   method

 

■ This method returns the current capacity (not size) of the Vector.  i.e. The snippet prints 10 because that's the default capacity. Capacity is different from the element count of three below.

 

import java.util.*;

 

Vector v = new Vector( );

v.add( "Huey" );

v.add( "Dewey" );

v.add( "Louie" );

System.out.println( v.capacity( ) );

 

 

void .copyInto( Obj[ ] )   method

 

■ This method copies the elements from the Vector into a previously created Array.  The Array must be large enough for all the elements you add or you will get an IndexOutOfBoundsException.  i.e. The snippet checks for a sufficiently large array, copies in two elements, and then prints  foo bar.

 

import java.util.*;

 

Vector v = new Vector( 2 );

v.add( "foo" );

v.add( "bar" );

String a[ ] = new String[ 2 ];                               // create the array

if ( a.length >= v.size( ) ) {                                    // check for sufficient array capacity

    v.copyInto( a );                                                 // copy the list into the array

    }

System.out.println( Arrays.asList( a ));  // print the array

 

 

Object .elementAt( int )   method

 

■ The .elementAt(...) method returns the element from the specified location in the Vector.  It is identical to the .get(...) method of List.     see .get(...) under  List interface

 

Vector v = new Vector( );

v.add( "AA" );

v.add( "BB" );

v.add( "CC" );

System.out.println( v.elementAt(2) );                               // prints CC

System.out.println( v.get(2) );                                           // prints CC

 

 

Enumeration .elements( )   method

 

■ The elements( ) method returns an Enumeration of this Vector.  see Enumeration

 

 

Object .firstElement( )   method

 

■ This method returns the first element of the Vector and is the same as saying v.get(0). The only difference is that .firstElement( ) throws a different exception if there are no elements present. See  .get(...)  under  List interface

 

 

void .insertElementAt( Obj, index )   method

 

■ This method inserts Objects the same as .add(...).  See .add(...)  under  List interface

 

 

Object .lastElement( )   method

 

■ Returns the last element from the Vector, without removing it, and without having to know the element's index position. That final element's index address is always  v.size( ) -1.  i.e.

 

import java.util.*;

 

Vector v = new Vector( );

v.add("element1");

v.add("element2");

v.add("element3");  then saying

v.lastElement( );  is the same as saying the following

v.get( v.size( ) -1 );  both statements return the 3rd element

 

 

void .removeAllElements( )   method

 

■ This method is the same as  .clear( )  under List interface

 

 

boolean .removeElement( Obj )   method

 

■ This method is the same as  .remove( Obj )  under List interface

 

 

void .removeElementAt( index )   method

 

■ Except for the fact that it doesn't return the removed element, this method is the same as .remove( Obj )  under  List interface

 

 

Object  .setElementAt( Obj, index )   method

 

■ Replaces the object at the given index position.  Except for the fact that it doesn't return the inserted element, and the fact that the order of its two parametersd are inverted, this method is the same as .set( index, Obj ) under List interface

 

 

void  .setSize( size )    method

 

■ This method will change the actual size of the Vector, adding end nulls or discarding end elements as needed.  i.e.

 

import java.util.*;

 

Vector v = new Vector( );

v.add( "AA" );

v.add( "BB" );

v.add( "CC" );

v.add( "DD" );

System.out.println(v);      // prints AA BB CC DD

v.setSize( 2 );

System.out.println(v);     // prints AA BB due to truncating

v.setSize( 4 );

System.out.println(v);     // prints AA BB null null due to lengthening

 

 

void .trimToSize( )  (same method works for ArrayList too)

 

■ For storage management optimization, you can "trim" any excess capacity off the end of your Vector by using this .trimToSize( ) method. 

 

■ Note this doesn't trim off any ending null elements, it just adjusts the capacity. 

i.e. Using the v from the example directly above:

 

v.trimToSize( ) ;

                                System.out.println(v);      // still prints AA BB null null