Java Vector add() Method



Description

The Java Vector add(E e) method is used to insert the specified element E at the end of the Vector. This method is available since Java 2 platform v1.2.

Declaration

Following is the declaration for java.util.Vector.add() method

public boolean add(E e)

Parameters

e − The element to be added in this Vector.

Return Value

This method returns true if the specified element is added successully into the Vector, otherwise it returns false.

Exception

NA

Java Vector add(int index,E element) Method

Description

This Java Vector add(int index,E element) method is used to Insert the specified element (E) at the specified position (index) in the list. While inserting the new element, it Shifts the existing element currently at that position (if any) and any subsequent elements to the right with changed indices.This is another variation of the previous add () method.

Declaration

Following is the declaration for java.util.Vector.add() method

public void add(int index,E element)

Parameters

  • index − This is the index (position) at which the specified element is to be inserted.

  • element − This is the element to be inserted at the specified location.

Return Value

The return type is void so the method does not return any value.

Exception

  • IndexOutOfBoundsException The method throws this exception if the index (element position) we are trying to access is out of range (index < 0 || index > size()).

Adding an Element to a Vector of Integer Example

The following example shows the usage of Java Vector add(E) method to add Integers. We're adding couple of Integers to the Vector object using add() method calls per element and then print each element to show the elements added.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty array list 
      Vector<Integer> vector = new Vector<>();

      // use add() method to add elements in the vector
      vector.add(20);
      vector.add(30);
      vector.add(20);
      vector.add(30);
      vector.add(15);
      vector.add(22);
      vector.add(11);

      // let us print all the elements available in vector
      for (Integer number : vector) {
         System.out.println("Number = " + number);
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Number = 20
Number = 30
Number = 20
Number = 30
Number = 15
Number = 22
Number = 11

Adding an Element to a Vector of String Example

The following example shows the usage of Java Vector add(E) method to add Strings. We're adding couple of strings to the Vector object using add() method calls per element and then printing the Vector using its toString() method.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty array list
      Vector<String> vector = new Vector<>();

      // use add() method to add elements in the vector
      vector.add("Welcome");
      vector.add("To");
      vector.add("Tutorialspoint");
      System.out.println("Vector = " + vector);      
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Vector = [Welcome, To, Tutorialspoint]

Adding an Element to a Vector of Object Example

The following example shows the usage of Java Vector add(index, E) method to add Student objects at particular index. We're adding couple of Student objects to the Vector object using add() method calls per element and using add(index, E) in the end to add a student at particular location and then printing the Vector using its toString() method.

package com.tutorialspoint;

import java.util.Vector;

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

      // create an empty vector
      Vector<Student> vector = new Vector<>();

      // use add() method to add elements in the vector
      vector.add(new Student(1, "Julie"));
      vector.add(new Student(2, "Robert"));
      vector.add(0, new Student(3, "Adam"));
      System.out.println("Vector = " + vector);      
   }
}

class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Vector = [[ 3, Adam ], [ 1, Julie ], [ 2, Robert ]]
java_util_vector.htm
Advertisements