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.
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.
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.
Vector: [10, 20, 30, 40, 50] The final vector is [100, 10, 20, 30, 200, 40, 50]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.