Java Vector addElement() method example

In this source code example, we will demonstrate the usage of the Vector addElement() method in Java with an example.

Vector Java.util.Vector.addElement() Method

Java.util.Vector.addElement(): This 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 the Vector class.

Java.util.Vector.addElement() Method Example

Below program illustrates the working of Java.util.Vector.addElement() method:

// Java code to illustrate boolean add(Object element) 
import java.util.*; 
    
public class VectorDemo { 
    public static void main(String args[]) 
    { 
    
        // Creating an empty Vector 
        Vector programLangs = new Vector(); 
    
        // Use add() method to add elements in the vector 
        programLangs.add("C"); 
        programLangs.add("C++"); 
        programLangs.add("Java"); 
        programLangs.add("Python"); 
        programLangs.add("Go"); 
    
        // Output the present vector 
        System.out.println("The vector is: " + programLangs); 
    
        /*
            * Adds the specified component to the end of this vector, increasing
            * its size by one. The capacity of this vector is increased if its size
            * becomes greater than its capacity.
            */
        programLangs.addElement("Scala"); 
        programLangs.addElement("JavaScript"); 
    
        // Printing the new vector 
        System.out.println("The new Vector is: " + programLangs); 
    } 
}    

Output:

The vector is: [C, C++, Java, Python, Go]
The new Vector is: [C, C++, Java, Python, Go, Scala, JavaScript]         

References

Comments