Vector addElement(K) in Java

addElement(K): This method is available in java.util.Vector class of Java.

Syntax:

void java.util.Vector.addElement(K obj)

This method takes one argument. This method 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.

Parameters: One parameter is required for this method.

obj: the component to be added.

Returns: NA

Exceptions: NA

Approach

Java

import java.util.Vector;

public class VectoraddElement {
    public static void main(String[] args) {

        Vector<String> vector = new Vector<>();

        vector.add("Hello");
        vector.add("Java");
        vector.add("C++");
        vector.add("Program");

        String obj = "Python";
        vector.addElement(obj);
        System.out.println(vector);
    }
}

Output:

[Hello, Java, C++, Program, Python]


No comments:

Post a Comment