Javatpoint Logo
Javatpoint Logo

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.

Syntax

Following is the declaration of insertElementAt() method:

Parameter

Parameter Description Required/Optional
obj It is the element which will be inserted. Required
index It is the index where we will insert the new element. Required

Return

The insertElementAt() method does not return anything. It only inserts an element at the given index.

Exceptions

ArrayIndexOutOfBoundsException- This method has thrown an exception if the index is out of range i.e. index < 0 || index >= size().

Compatibility Version

Java 1.2 and above

Example 1

Test it Now

Output:

Element in vector before insertion = [10, 20, 30, 40, 50]
Element in vector after insertion = [10, 20, 700, 30, 40, 50]

Example 2

Test it Now

Output:

Components of vector: 
 Java
 Ruby
 Android
 Python
Components of vector after insertion = 
 Java
 PHP
 Ruby
 Android
 Python

Example 3

Test it Now

Output:

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)

Next TopicJava Vector





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA