Vector addElement() Method in Java
Last Updated :
09 Dec, 2024
Improve
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.
Example of Vector addElement() Method
Below programs illustrate the working of java.util.Vector.add(Object element) method.
Example 1 :
// Java Program to illustrate
// boolean addElement()
import java.util.*;
public class GFG
{
public static void main(String args[])
{
// Creating an empty Vector
Vector<String> v = new Vector<String>();
// Add elements in the vector
v.add("Geeks");
v.add("for");
v.add("Geeks");
// Output the present vector
System.out.println("The vector is : " + v);
// Adding new elements to the end
v.addElement("Last");
v.addElement("Element");
// Printing the new vector
System.out.println("The new Vector is : " + v);
}
}
Output
The vector is : [Geeks, for, Geeks] The new Vector is : [Geeks, for, Geeks, Last, Element]
Example 2:
// Java Program to illustrate
// boolean addElement()
import java.util.*;
public class VectorDemo
{
public static void main(String args[])
{
// Creating an empty Vector
Vector<Integer> v = new Vector<Integer>();
// Add elements in the vector
v.add(1);
v.add(2);
v.add(3);
// Output the present vector
System.out.println("The vector is : " + v);
// Adding new elements to the end
v.addElement(50);
v.addElement(100);
// Printing the new vector
System.out.println("The new vector is : " + v);
}
}
Output
The vector is : [1, 2, 3] The new vector is : [1, 2, 3, 50, 100]