Add elements to a Vector in Java



The java.util.Vector class is a part of the Java Collections Framework. It implements the List interface and is used for storing the elements in a dynamic array.

It is similar to an ArrayList but is synchronized (thread-safe). The size of a vector grows dynamically as we add more elements to it and shrinks when we remove elements. It can also contain duplicate elements and null values.

We can add elements to a vector in the following ways:

  • Using add() Method
  • Using add(int index, E element) Method
  • Using addElement() Method

Let's understand each of these methods one by one.

Using add() Method

The add() method of the Vector class accepts a single element as a parameter and adds it to the current vector object. It returns a Boolean value, which is TRUE if the element is added to the Vector and FALSE if the element is not added.

Example

In the following example, we will create a vector and add some elements to it using the add() method.

import java.util.Vector;
public class AddElementsToVector {
   public static void main(String[] args) {
      Vector vector = new Vector<>();

      // Adding elements to the vector
      vector.add("Apple");
      vector.add("Banana");
      vector.add("Orange");
      System.out.println("Vector contains: " + vector);
   }
}

The output of the above program is as follows:

Vector contains: [Apple, Banana, Orange]

Using add(int index, E element) Method

The add(int index, E element) method is used for adding a single element, just like the add() method. But with this method, we can add an element at a specific index in the vector. This method has two parameters: an index value where we want to add the element, and the element to be added.

Example

In the following example, we will create a vector and add some elements at indices 3 and 4 using the add(int index, E element) method.

import java.util.Vector;
public class AddElementsToVector {
   public static void main(String[] args) {
      Vector vector = new Vector<>();

      // Adding elements to the vector
      vector.add("Apple");
      vector.add("Banana");
      vector.add("Orange");

      // Adding elements at specific index
      vector.add(3, "Grapes");
      vector.add(4, "Mango");
      System.out.println("Vector contains: " + vector);
   }
}

Following is the output of the above program:

Vector contains: [Apple, Banana, Orange, Grapes, Mango]

As you can see, we added "Grapes" at index 3 and "Mango" at index 4. The vector adjusts the size by itself and adds the elements at the specified index.

Using addElement() Method

The addElement() method in Java is similar to the add() method. It accepts an element as a parameter and adds it to the current Vector object.

Example

In the following example, we will create a vector and add some elements to it using the addElement() method.

import java.util.Vector;
public class AddElementsToVector {
   public static void main(String[] args) {
      Vector vector = new Vector<>();

      // Adding elements to the vector
      vector.addElement("Apple");
      vector.addElement("Banana");
      vector.addElement("Orange");
      System.out.println("Vector contains: " + vector);
   }
}

Following is the output of the above program:

Vector contains: [Apple, Banana, Orange]
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-07-17T17:54:23+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements