The Wayback Machine - https://web.archive.org/web/20240618020216/https://www.geeksforgeeks.org/vector-addelement-method-in-java/
Open In App

Vector addElement() Method in Java

Last Updated : 02 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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]

 



Previous Article
Next Article

Similar Reads

Stack addElement(E) method in Java with Example
The addElement(E) method of Stack Class is used to append the element passed as a parameter to this function at the end of the Stack. Syntax: boolean addElement(E obj) Here, E is the type of elements maintained by this container. Parameters: This function accepts a parameter E obj which is the object to be added at the end of the Stack. Return Valu
2 min read
Copy Elements of One Java Vector to Another Vector in Java
Vector is similar to arrays but is growable also, or we can say no fixed size is required. Previously vector was a part of legacy classes but now it is part of Collections. It also implements a List interface, so we can use any method of list interface on vectors also. Syntax : Vector&lt;Integer&gt; gfg=new Vector&lt;&gt;(); Ways To copy elements o
3 min read
Vector clear() Method in Java
The Java.util.Vector.clear() method is used to remove all the elements from a Vector. Using the clear() method only clears all the element from the vector and does not delete the vector. In other words, we can say that the clear() method is used to only empty an existing vector. Syntax: Vector.clear() Parameters: The method does not take any parame
2 min read
Vector addAll() Method in Java
java.util.Vector.addAll(Collection C): This method is used to append all of the elements from the collection passed as a parameter to this function to the end of a vector keeping in mind the order of return by the collection's iterator. Syntax: boolean addAll(Collection C) Parameters: The method accepts a mandatory parameter C which is a collection
2 min read
Vector set() Method in Java
The Java.util.Vector.set() method is used to replace any particular element in the vector, created using the Vector class, with another element. Syntax: Vector.set(int index, Object element) Parameters: This function accepts two mandatory parameters as shown in the above syntax and described below. index: This is of integer type and refers to the p
2 min read
Vector isEmpty() Method in Java
The Java.util.Vector.isEmpty() method in Java is used to check and verify if a Vector is empty or not. It returns True if the Vector is empty else it returns False. Syntax: Vector.isEmpty() Parameters: This method does not take any parameter. Return Value: This function returns True if the Vectoris empty else it returns False. Below programs illust
2 min read
Vector lastIndexOf() Method in Java
The Java.util.Vector.lastIndexOf(Object element) method is used to check and find the occurrence of a particular element in the vector. If the element is present in the vector then the lastIndexOf() method returns the index of last occurrence of the element otherwise it returns -1. This method is used to find the last occurrence of a particular ele
2 min read
Vector subList() Method in Java
The java.util.Vector.subList() is used to return a sublist of the existing Vector within a range mentioned in the parameter. The method takes in an upper limit and a lower limit and returns all the elements mentioned in the range. The lower limit is included if the element is present in the list and the upper limit is excluded. Basically, it takes
3 min read
Vector add() Method in Java
boolean add(Object element): This method appends the specified element to the end of this vector. Syntax: boolean add(Object element) Parameters: This function accepts a single parameter element as shown in the above syntax. The element specified by this parameter is appended to end of the vector. Return Value: This method returns True after succes
2 min read
Vector capacity() Method in Java
The Java.util.Vector.capacity() method in Java is used to get the capacity of the Vector or the length of the array present in the Vector. Syntax: Vector.capacity() Parameters: The method does not take any parameter. Return Value: The method returns the capacity or the internal data array’s length present in the Vector, which is an integer value. B
2 min read