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
import java.util.*;
public class GFG {
public static void main(String args[])
{
Vector<String> vec_tor = new Vector<String>();
vec_tor.add("Geeks");
vec_tor.add("for");
vec_tor.add("Geeks");
vec_tor.add("10");
vec_tor.add("20");
System.out.println("The vector is: " + vec_tor);
vec_tor.addElement("Last");
vec_tor.addElement("Element");
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
import java.util.*;
public class VectorDemo {
public static void main(String args[])
{
Vector<Integer> vec_tor = new Vector<Integer>();
vec_tor.add(1);
vec_tor.add(2);
vec_tor.add(3);
vec_tor.add(10);
vec_tor.add(20);
System.out.println("The vector is: " + vec_tor);
vec_tor.addElement(50);
vec_tor.addElement(100);
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]
Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
02 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...