Javatpoint Logo
Javatpoint Logo

Java Vector set() Method

The set() method of Java Vector class is used to replace the element at the specified position in the vector with the specified element.


Following is the declaration of set() method:


Parameter Description Required/Optional
index It is an index of the element which will be replaced. Required
element It is an element which will be stored at the specified position. Required


The set() method returns the element that was previously stored at the specified position.


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

Compatibility Version

Java 1.2 and above

Example 1

Test it Now


Vector element before set: [1, 2, 3, 4, 6]
Vector element after set: [1, 2, 3, 4, 5]

Example 2

Test it Now


Vector element before set: [Java, Android, Python, COBOL]
Vector element after set: [Java, Android, Python, JavaTpoint]

Example 3

Test it Now


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 15
	at java.base/java.util.Vector.set(
	at myPackage.VectorSetExample3.main(

Youtube For Videos Join Our Youtube Channel: Join Now


  • Send your Feedback to

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA