Java Vector insertElementAt() Method
The insertElementAt() method of Java Vector class is used to insert the specified object as a component in this vector at the specified index. Each component in the vector with an index greater or equal to the given index is shifted upward. Now, the vector index is one greater than the value it had previously.
Following is the declaration of insertElementAt() method:
The insertElementAt() method does not return anything. It only inserts an element at the given index.
ArrayIndexOutOfBoundsException- This method has thrown an exception if the index is out of range i.e. index < 0 || index >= size().
Java 1.2 and above
Example 1Test it Now
Element in vector before insertion = [10, 20, 30, 40, 50] Element in vector after insertion = [10, 20, 700, 30, 40, 50]
Example 2Test it Now
Components of vector: Java Ruby Android Python Components of vector after insertion = Java PHP Ruby Android Python
Example 3Test it Now
Color elements in vector: [White, Green, Black] Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12 > 3 at java.base/java.util.Vector.insertElementAt(Vector.java:619) at myPackage.VectorInsertElementAtExample3.main(VectorInsertElementAtExample3.java:14)