Vector
Table of Contents
- 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 |
|
|
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) ;
}
}

Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.


