Java Vector add() Method with Examples

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

In this article we are going to see add() method to add elements into a vector by using Java programming language.

Java Program to Add Elements into a Vector

Let’s see Vector Add() method with suitable examples.

Boolean Add( ):

This java.util.Vector.Add(Object element) method adds the element to the end of the vector and returns true on successful addition or false.

Syntax-

vectorName.add(Object element)

Where,

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

Approach:

  • Initialize a vector of type String.
  • Add some elements into the vector using the add method.
  • Display the vector elements.
  • Initialize a boolean value and call the boolean add method to add an element “Element 1” to the vector.
  • Use an if-else condition to demonstrate the successful addition of the element.
  • Print the new vector.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args) 
    {
        // Create a Vector of string datatype
        Vector<String> vec = new Vector<String>();
        // 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
        System.out.println("The vector elements are "+vec);
        // Adding a new element to the vector
        boolean b = vec.add("Element 1");
        // If-else to demonstrate the boolean value
        // Checks if the element has been added to the vector sucessfully
        if(b)
            System.out.println("The element {"+vec.lastElement()+"} has been added sucessfully");
        else
            System.out.println("The element could not be added");
        // Prints the vector elements
        System.out.println("The new vector is"+vec);
    }
}
Output:

The vector elements are [Hello, this, is, a, vector]
The element {Element 1} has been added successfully
The new vector is[Hello, this, is, a, vector, Element 1]

Void Add( ):

This java.util.Vector.Add(int Index, Object element) method adds the element to a specified index of the vector. The return type of this method is void hence it does not return any thing.

Syntax-

 vectorName.add(int Index, Object element)

Where,

  • vectorName refers to the name of your Vector.
  • int Index refers to the index of vector where the new element will be added.
  • Object element refers to the element that will be added into the vector.

Approach:

  • Initialize a vector of type string.
  • Add some elements into the vector using the add method.
  • Display the vector elements.
  • Add an element to the vector at index 0 using the add(int Index, Object element) method.
  • Print the modified vector.
  • Add another element at index.
  • Print the new vector.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args) 
    {
        // Create a Vector of string datatype
        Vector<String> vec = new Vector<String>();
        // 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
        System.out.println("The vector elements are "+vec);
        // Adding a new element to the vector at index 0
        vec.add(0,"Element 1");
        // Prints the vector elements
        System.out.println("The new vector is"+vec);
        // Adding another element to the vector at index 0 again
        vec.add(0,"Element 0");
        // Prints the vector elements
        System.out.println("The new vector is"+vec);
    }
}
Output:

The vector elements are [Hello, this, is, a, vector]
The new vector is[Element 1, Hello, this, is, a, vector]
The new vector is[Element 0, Element 1, Hello, this, is, a, vector]

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Programs: