- The underlying data structure is re-sizable array or growable array.
- Insertion order is preserved.
- Duplicate objects are allowed
- Null insertion is possible.
- Heterogeneous objects are allowed.
- Implements serializable, cloneable & Random Access Interfaces,
- Best suitable if our frequent operation is retrieval & worst choice if our frequent operation is insertion or deletion in the middle.
- Every method in vector is synchronized. Hence vector object is thread safe.
Constructors of Vector:-
- Vector v = new vector();
- Creates an empty object with default initial capacity IO.
- Once vector reaches its max. capacity a new vector object will be created with double capacity
New capacity = 2 * Current capacity
2. Vector v = new vector (int initialcapacity);
3 .Vector v = new vector (int initialcapacity, int incrementalcapicity);
4. Vector v = new vector (collection c);
Vector Specific methods :
- To add objects
- Add(object o)
- Add(int index, object o)
- AddElement(object obj)
- To remove elements or objects
- Remove(object o)
- RemoveElement(object o)
- Remove(int index)
- removeElementAt(int index)
- clear()
- removeAllElements()
- To retrieve elements
- Get(int index)
- elementAt(int index)
- firstElement();
- LastElement();
- Other methods
- Int size();
- Int capacity();
- Enumeration elements();
Example of Vector:-
Import java.util.*; Class Demo { public static void main(String[] arg) { Vector v = new vector r(); System.out.println(v.capacity()); For (int i = 1; k = 10, i = ++) { v.addElement(i); } System.out.println(v.capacity()); v.addElement(“A”); System.out.println(v.capacity()); System.out.println(v); } }