The
Vectorclass is a growable array of objects. The elements ofVectorcan be accessed using an integer index and the size of aVectorcan be increased or decreased. Read more aboutVectorhere.
The addElement() method of the Vector class can be used to add an element at the end of the vector object.
public void addElement(obj)
The element to be added to the Vector object is passed as an argument obj.
This method doesn’t return any value.
import java.util.Vector;class Add {public static void main( String args[] ) {// Creating VectorVector<Integer> vector = new Vector<>();// add elementsvector.addElement(1);vector.addElement(2);vector.addElement(3);vector.addElement(4);// Print vectorSystem.out.println("The Vector is: " + vector);// Adding new elements to the endvector.addElement(5);// Printing the new vectorSystem.out.println("The Vector is: " + vector);}}
In the code above:
We created a new object for the Vector class with the name vector.
Then, we added the elements 1,2,3,4 to the vector object using the addElement method. The elements are added to the end of the vector object.
We again used the addElement method to add the element 5 to the end of the vector.