Java Vector - addElement() Method
The java.util.Vector.addElement() method is used to add a new element to the end of the vector. Addition of new element results into increasing the vector size by one.
Syntax
public void addElement (E element)
Here, E is the type of element maintained by the container.
Parameters
element |
Specify the element which need to be added in the vector. |
Return Value
void type.
Exception
NA
Example:
In the example below, the java.util.Vector.addElement() method is used to add a new element to the end of the vector.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a vector Vector<Integer> MyVector = new Vector<Integer>(); //populating vector MyVector.addElement(10); MyVector.addElement(20); MyVector.addElement(30); //printing vector System.out.println("MyVector contains: " + MyVector); } }
The output of the above code will be:
MyVector contains: [10, 20, 30]
❮ Java.util - Vector