The add()
method of the Vector
class can be used to add an element at the end of the vector object.
Vector
is a growable array of objects. Elements are mapped to an index.
public boolean add(E element);
This method returns true
if the Vector
changes as a result of the call. Otherwise, it returns false
.
import java.util.Vector;class VectorAddExample {public static void main( String args[] ) {// Creating VectorVector<Integer> vector = new Vector<>();// add elememtsvector.add(1);vector.add(2);vector.add(3);vector.add(4);// Print vectorSystem.out.println("The Vector is: " + vector);// Adding new elements to the endvector.add(5);// Printing the new vectorSystem.out.println("The Vector is: " + vector);}}
In the code above:
We create a Vector
with the name vector
.
We add elements, 1,2,3,4
, to the vector
with the add
method. The elements are added to the end of the vector
object.
We use the add
method again to add 5
as the last element of the vector
.