Vector insertElementAt() Method in Java

The java.util.vector.insertElementAt(element, index) method is used to insert a particular element at the specified index of the Vector. Both the element and the position is passed as the parameters. If an element is inserted at a specified index, then all the elements are pushed upward by one and hence the capacity is increased, creating a space for the new element.

Syntax:

Vector.insertElementAt()

Parameters: The method accepts two parameters:

  • element: This is element required to be inserted into the vector.
  • index: This is of integer type and refers to the position where the new element is to be inserted.

Return Value: The method does not return anything.

Exception: The method throws ArrayIndexOutOfBoundsException if the index is an invalid number.

Below programs illustrate the Java.util.Vector.insertElementAt() method:

Program 1: Adding String elements into the Vector.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate insertElementAt()
import java.util.*;
  
public class VectorDemo {
    public static void main(String args[])
    {
        // Creating an empty Vector
        Vector<String> vec_tor = new Vector<String>();
  
        // Use add() method to add elements into the Vector
        vec_tor.add("Welcome");
        vec_tor.add("To");
        vec_tor.add("Geeks");
        vec_tor.add("4");
        vec_tor.add("Geeks");
  
        // Displaying the Vector
        System.out.println("Vector: " + vec_tor);
  
        // Inseting element at 3rd position
        vec_tor.insertElementAt("Hello", 2);
  
        // Inseting element at last position
        vec_tor.insertElementAt("World", 6);
  
        // Displaying the final Vector
        System.out.println("The final vector is " + vec_tor);
    }
}

chevron_right


Output:

Vector: [Welcome, To, Geeks, 4, Geeks]
The final vector is [Welcome, To, Hello, Geeks, 4, Geeks, World]

Program 2: Adding Integer elements into the Vector.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate insertElementAt()
import java.util.*;
  
public class VectorDemo {
    public static void main(String args[])
    {
        // Creating an empty Vector
        Vector<Integer> vec_tor = new Vector<Integer>();
  
        // Use add() method to add elements into the Vector
        vec_tor.add(10);
        vec_tor.add(20);
        vec_tor.add(30);
        vec_tor.add(40);
        vec_tor.add(50);
  
        // Displaying the Vector
        System.out.println("Vector: " + vec_tor);
  
        // Inseting element at 1st position
        vec_tor.insertElementAt(100, 0);
  
        // Inseting element at fifth position
        vec_tor.insertElementAt(200, 4);
  
        // Displaying the final Vector
        System.out.println("The final vector is " + vec_tor);
    }
}

chevron_right


Output:

Vector: [10, 20, 30, 40, 50]
The final vector is [100, 10, 20, 30, 200, 40, 50]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.