The Vector class of java

The java Vector class implements an array of objects similar to ArrayList.  Vector elements are accessible through an integer index. The size of a Vector varies according to the need (to add or remove elements) after creation.

Vector is synchronized, which means that it allows several operations at the same time, however, it does not guarantee good performance when using multi-threading. It is recommended to use ArrayList which gives good performance if you will not need to synchronize the list.

Vector implements the List interface like ArrayList, its disadvantage is that it gives poor performance as mentioned because of its synchronization in the operations of adding, finding, deleting and modifying these elements.

The Vector class supports 4 constructors:

1)Vector v = new Vector();
This constructor creates an empty vector with an initial size of 10. The vector will be resized if it exceeds 10 elements so that the 11th element is inserted into Vector.

2)Vector v = new Vector(int capabilityInitial);
This shape initializes the vector with an initial size that specifies the number of elements to allocate.
Vector v = new Vector(5);//It will create a Vector with capacity 5.

3)Vector v = new Vector(int capabilityInitial, int capacityIncrement)
Creates a Vector with an initial capacity and specifies how many tiles will be allocated when Vector resizes the capacity if the number of items reaches the maximum size.
Vector v = new Vector(8);
It will create a Vector with a capacity of 8 and an increment capacity of 4. What does this mean that inserting the 9th element, the size is 12(8+4) and the 16th insertion is going to be 20(16+4).

4)Vector v = new Vector(Collection c);
Initializes the vector with a collection of objects (ArrayList, TreeSet, HashMap, etc).

Vector's important methods

As in ArrayList and other object collections, Vector implements the Interface List  and the interface  Collections from which it inherits many methods of adding, deleting, searching, modifying.

Add Item

Adding is done with two methods:  add() and addElement(). addElement() after inserting increments the size of the list.

import java.util.Vector; 

public class addElement{

public static void main(String[] args) {
Vector v = new Vector();

v.add(1);
v.add(2);
v.add(3);
System.out.println(v);
v.addElement(4);
System.out.println(v);
}
}
Output:

[1, 2, 3]
[1, 2, 3, 4]

Delete an element

You can delete either with the index with the < method span style="font-family: Courier New, Courier, minivan;" >remove(int index), either with your own method of Vector removeElementAt(int index). Searching for and deleting an object is possible with the method remove(Object o):

import java.util.Vector; 

public class remove{

public static void main(String[] args) {
Vector v = new Vector();

v.add(1);
v.add(2);
v.add(3);
v.add(4);
System.out.println(v);
//delete with index
v.remove(2);
System.out.println("deletion of the element at index 2: "+v);
//suppressioin with object
v.remove(new Integer(1));
System.out.println("delete integer 1:"+v);
}
}
Output:

[1, 2, 3, 4]
delete element at index 2: [1, 2, 4]
delete integer 1:[2, 4]
There is also the removeAll(Collection c) which removes a subset from the list. To empty the list use the method removeAll() which deletes all elements.

elementAt() and setElementAt()

get(int i)  or elementAt(int i) returns the element to the defined index i and set(Object o,  int i) or setElementAt(Object obj, int i) replaces the element with the object obj. get() is often used to flip elements as Vector travels. The capacity is obtained with the method size().

import java.util.Vector; 

public class Test {

public static void main(String[] args) {
Vector v = new Vector();

v.add(1);
v.add(2);
v.add(3);
v.add(4);

System.out.println("first element: "+v.firstElement());
System.out.println("last element: "+v.lastElement());

for(int i = 0; i< v.size(); i++)
System.out.println(v.get(i));

v.setElementAt("two", 1);
System.out.println(v);
}
}
Output:

first element: 1
last element: 4
1
2
3
4
[1, two, 3, 4]

Search for an element

Searching in a vector is easy, you have to call the method indexOf(Object o) which returns the index of the first occurrence found. We also have the method lastIndexOf(Object o) which returns the index of the last occurrence found.

import java.util.Vector; 

public class Test {

public static void main(String[] args) {
Vector v = new Vector();

v.add("java");
v.add("c");
v.add("c++");
v.add("javascript");
v.add("java");

System.out.println("first occurrence of c: "+v.indexOf("c++"));
System.out.println("last occurrence of java: "+v.lastIndexOf("java"));
}
}
Output

first occurrence of c: 2
last occurrence of java: 4

Other methods:

- int Capacity(): returns the capacity of the list.
- int setSize(): change the current size of the list.
- boolean contains(Object o): check if vector contains the object o.
- boolean isEmpty(): returns true if empty.
- Object[] toArray(): converted vector to an array of objects.

note:
size() returns the size of the list and capacity() returns the capacity that the list can accommodate. So the Vector path is always done with size() otherwise you will see an error that says you have exceeded the size.

References:
openclassroms vector java
javadoc: vector class