Site icon Narayana Tutorial

Vector

Vector

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.

⇒For accessing objects:-

⇒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) ;
        }
}