Vector

Vector

  • The underlying data structure is resizable or growable array.
  • Insertion order is preserve.
  • Duplicate objects allow.
  • Null insertion is possible.
  • Heterogeneous objects are allow.
  • Vector class implements Serialization, Cloneable & RandomAccess  interface.
  • Best suitable for frequent operation is retrieval.
  • Worst choice is Frequent operation is insertion or deletion on middle.
  • Every method present in vector class is synchronized. Hence vector objects thread safe.

Difference between ArrayList and  Vector:-

ArrayList

Vector

  1. No method is synchronized.
  2. ArrayList object is not thread sage.
  3. Introduced in 1.2 version and hence non legacy.
  4. Performance is high.

 

 

 

  1. All methods are synchronized.
  2. Vector object is always thread sage.
  3. Introduced in 1.0 version and hence vector legacy.
  4. Performance is low.

 

 

 

Collections class contains synchronized list () method
public static List synchronizedList (list l)
ArrayList l = new ArrayList ();
List l2 = collections synchronizedList ( l ) ;// non synchronized list

Vector class methods

⇒For adding objects:-

add ( object o ) // collection

add ( int index, object o ) // List

addElement ( object o );  // vector

⇒For removing objects:-

Remove of objects from collection.

  • remove ( object o ) ; // collection
  • remove  ( int index ) ; // List
  • removeElement ( object o ); // vector
  • clear () ; → collection
  • removeElementAt ( int index ) // vector
  • removeAllElements () ; // vector

⇒For accessing objects:-

  • get ( int index) // List
  • ElementAt ( int index ) // vector
  • FirstElement ()  // vector
  • LastElement () // vector

⇒Other general methods:-

int size ()

int capacity ()

Enumeration element ()

vector v = new vector ();
creates an empty vector object with default initial capacity 10. 
If vector reaches its maximum capacity then new vector object will be
created with double capacity.
vector v = new vector ( int initial capacity )
vector v = new vector ( int initial capacity, int incremental capacity )
vector v = new vector ( collection c )
import java. util.* ;
Class VectorDemo
{
public static void main ( Sting[] args )
     {
         vector v = new vector () ;
         s.o.p ( vector capacity () ); // 10
         for (int i = 1 ; i < = 10; i ++ )
         {
              vector addElement ( i ) ;
         }
         system. out.printIn(v); // 10
         vector addElement ( "A" );
         system. out. prontIn ( vector cpacity () ) ; // 10
         system. out. printIn ( v) ;
        }
}

Leave a Reply