Open In App

Vector insertElementAt() Method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

insertElementAt() method of Vector class present inside java.util package is used to insert a particular element at the specified index of the Vector. Both the element and the position are 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: It is required to be inserted into the vector.
  • index: It refers to the position where the new element is to be inserted(integer type)

Exceptions Thrown: ArrayIndexOutOfBoundsException if the index is an invalid number.

Let us now add elements by proposing examples for both string elements and integer elements and applying our method over both of them just only to be familiar with the working of this method with different primitive datatypes.

Example 1:

Java




// Java Program to illustrate insertElementAt()
// Method of Vector class by
// Adding String elements into the Vector
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Creating an empty vector of string type
        Vector<String> vec_tor = new Vector<String>();
 
        // Adding custom elements into the vector
        // using add() method
        vec_tor.add("Welcome");
        vec_tor.add("To");
        vec_tor.add("Geeks");
        vec_tor.add("4");
        vec_tor.add("Geeks");
 
        // Printing elements of vector
        System.out.println("Vector: " + vec_tor);
 
        // Inserting element at 3rd position
        // Custom specified
        vec_tor.insertElementAt("Hello", 2);
 
        // Inserting element at last position
        vec_tor.insertElementAt("World", 6);
 
        // Printing elements of final vector
        System.out.println("The final vector is "
                           + vec_tor);
    }
}


Output: 

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

 

Example 2:

Java




// Java Program to illustrate insertElementAt()
// Method of Vector class by
// Adding Integer Elements into the Vector
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Creating an empty Vector of integer type
        Vector<Integer> vec_tor = new Vector<Integer>();
 
        // Adding elements into the vector
        // using add() method
        vec_tor.add(10);
        vec_tor.add(20);
        vec_tor.add(30);
        vec_tor.add(40);
        vec_tor.add(50);
 
        // Printing the current elements of vector
        System.out.println("Vector: " + vec_tor);
 
        // Inserting element at 1st position
        vec_tor.insertElementAt(100, 0);
 
        // Inserting element at 5th position
        vec_tor.insertElementAt(200, 4);
 
        // Printing the final elements of Vector
        System.out.println("The final vector is "
                           + vec_tor);
    }
}


Output: 

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

 

 



Last Updated : 24 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads