Saturday, September 6, 2025
HomeLanguagesJavaVector addElement() Method in Java

Vector addElement() Method in Java

The Java.util.Vector.addElement() method is used to append a specified element to the end of this vector by increasing the size of the vector by 1. The functionality of this method is similar to that of the add() method of Vector class.
Syntax: 
 

boolean addElement(Object element)

Parameters: This function accepts a single parameter element of object type and refers to the element specified by this parameter is appended to end of the vector. 
Return Value: This is a void type method and does not returns any value.
Below programs illustrate the working of java.util.Vector.add(Object element) method.
Program 1 : 
 

Java




// Java code to illustrate boolean addElement()
import java.util.*;
 
public class GFG {
    public static void main(String args[])
    {
 
        // Creating an empty Vector
        Vector<String> vec_tor = new Vector<String>();
 
        // Use add() method to add elements in the vector
        vec_tor.add("Geeks");
        vec_tor.add("for");
        vec_tor.add("Geeks");
        vec_tor.add("10");
        vec_tor.add("20");
 
        // Output the present vector
        System.out.println("The vector is: " + vec_tor);
 
        // Adding new elements to the end
        vec_tor.addElement("Last");
        vec_tor.addElement("Element");
 
        // Printing the new vector
        System.out.println("The new Vector is: " + vec_tor);
    }
}


Output: 

The vector is: [Geeks, for, Geeks, 10, 20]
The new Vector is: [Geeks, for, Geeks, 10, 20, Last, Element]

 

Program 2: 
 

Java




// Java code to illustrate boolean addElement()
import java.util.*;
 
public class VectorDemo {
    public static void main(String args[])
    {
 
        // Creating an empty Vector
        Vector<Integer> vec_tor = new Vector<Integer>();
 
        // Use add() method to add elements in the vector
        vec_tor.add(1);
        vec_tor.add(2);
        vec_tor.add(3);
        vec_tor.add(10);
        vec_tor.add(20);
 
        // Output the present vector
        System.out.println("The vector is: " + vec_tor);
 
        // Adding new elements to the end
        vec_tor.addElement(50);
        vec_tor.addElement(100);
 
        // Printing the new vector
        System.out.println("The new Vector is: " + vec_tor);
    }
}


Output: 

The vector is: [1, 2, 3, 10, 20]
The new Vector is: [1, 2, 3, 10, 20, 50, 100]

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS