Java Vector addElement() Method with Examples

In the previous article we have discussed Java Vector add() Method with Examples

In this article you will see the use of addElement() of Vector in Java along with suitable examples.

Java Vector addElement() Method with Examples

Void addElement( ):

This java.util.Vector.addElement() method adds the element to the end of the vector by increasing the size of the vector by 1. The return type of this method is void so it does not return any value.

Syntax-

vectorName.addElement(Object element)

Where,

  • vectorName refers to the name of your Vector.
  • Object element refers to the element that will be added into the vector.

Let’s see different examples to understand it more clearly.

Method-1: Java Vector addElement() Method – Example with String Type Vector

Approach:

  • Initialize a vector of type String of size 5.
  • Add 5 elements into the vector using the add() method.
  • Display the vector elements and the size of the vector.
  • Add an element to the vector using addElement( ).
  • Print the new vector with its size.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a Vector of string datatype with size 5
        Vector<String> vec = new Vector<String>(5);
        // Adding some elements to the vector
        vec.add("Hello");
        vec.add("this");
        vec.add("is");
        vec.add("a");
        vec.add("vector");
        // Prints the vector elements and vector size
        System.out.println("The size of the vector is "+vec.size()+" and the elements are "+vec);
        // Adding a new element to the vector
        vec.addElement("Element 1");
        // Prints the vector elements and the vector size
        System.out.println("The new vector is "+vec+" of size "+vec.size());
    }
}
Output:

The size of the vector is 5 and the elements are [Hello, this, is, a, vector]
The new vector is [Hello, this, is, a, vector, Element 1] of size 6

Method-2: Java Vector addElement() Method – Example with Integer Type Vector

Approach:

  • Initialize a vector of type Integer of size 6.
  • Add 5 elements into the vector using the add() method.
  • Display the vector elements and the size of the vector.
  • Add an element to the vector using addElement( ).
  • Print the new vector with its size.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a Vector of Integer datatype with size 6
        Vector<Integer> vec = new Vector<Integer>(6);
        // Adding some elements to the vector
        vec.add(11);
        vec.add(65);
        vec.add(82);
        vec.add(27);
        vec.add(89);
        vec.add(46);
        // Prints the vector elements and vector size
        System.out.println("The size of the vector is "+vec.size()+" and the elements are "+vec);
        // Adding a new element to the vector
        vec.addElement(58);
        // Prints the vector elements and the vector size
        System.out.println("The new vector is "+vec+" of size "+vec.size());
    }
}
Output:

The size of the vector is 6 and the elements are [11, 65, 82, 27, 89, 46]
The new vector is [11, 65, 82, 27, 89, 46, 58] of size 7

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: