Vector insertElementAt() Method in Java with Examples
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.
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.
Vector: [Welcome, To, Geeks, 4, Geeks] The final vector is [Welcome, To, Hello, Geeks, 4, Geeks, World]
Vector: [10, 20, 30, 40, 50] The final vector is [100, 10, 20, 30, 200, 40, 50]
Please Login to comment...