Java Utility Library

Java Vector - add() Method



The java.util.Vector.add() method is used to add a new element at the end of the vector.

Syntax

public boolean add(E element)

Here, E is the type of element maintained by the container.


Parameters

element Specify element which need to be added in the vector.

Return Value

Returns true if the element is successfully added, else returns false.

Exception

NA

Example:

In the example below, the java.util.Vector.add() method is used to add new element at 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 using addElement() method
    MyVector.addElement(10);
    MyVector.addElement(20);
    MyVector.addElement(30);

    //populating vector using add() method
    MyVector.add(100);
    MyVector.add(200);

    //printing vector
    System.out.println("MyVector contains: " + MyVector);   
  }
}

The output of the above code will be:

MyVector contains: [10, 20, 30, 100, 200]

❮ Java.util - Vector