The setElementAt() method of Java Vector is used to set the component at the specified index of this vector to be the specified object. The previous component at that position is discarded. The index must be a value greater than or equal to 0 and less than the current size of the vector.
Syntax:
public void setElementAt(E element, int index)
Parameters: This function accepts two parameters as shown in the above syntax and described below.
- element: It is the new element by which the existing element will be replaced and is of the same object type as the vector.
- index: This is of integer type and refers to the position of the element that is to be replaced from the vector.
Return Value: This method do not return anything.
Exception: This method throws ArrayIndexOutOfBoundsException if the index is out of range (index = size())
Below program illustrate the Java.util.Vector.setElementAt() method:
Example 1:
// Java code to illustrate setElementAt() import java.io.*; import java.util.*; public class VectorDemo { public static void main(String args[]) { // Creating an empty Vector Vector<String> vector = new Vector<String>(); // Use add() method to add elements in the vector vector.add("Geeks"); vector.add("for"); vector.add("Geeks"); vector.add("10"); vector.add("20"); // Displaying the linkedvector System.out.println("Vector:" + vector); // Using setElementAt() method to replace Geeks with GFG vector.setElementAt("GFG", 2); System.out.println("Geeks replaced with GFG"); // Displaying the modified linkedvector System.out.println("The new Vector is:" + vector); } } |
Vector:[Geeks, for, Geeks, 10, 20] Geeks replaced with GFG The new Vector is:[Geeks, for, GFG, 10, 20]
Example 2: To demonstrate ArrayIndexOutOfBoundsException
// Java code to illustrate setElementAt() import java.io.*; import java.util.*; public class VectorDemo { public static void main(String args[]) { // Creating an empty Vector Vector<String> vector = new Vector<String>(); // Use add() method to add elements in the vector vector.add("Geeks"); vector.add("for"); vector.add("Geeks"); vector.add("10"); vector.add("20"); // Displaying the linkedvector System.out.println("Vector:" + vector); // Using setElementAt() method to replace 10th with GFG // and the 10th element does not exist System.out.println("Trying to replace 10th " + "element with GFG"); try { vector.setElementAt("GFG", 10); } catch (Exception e) { System.out.println(e); } } } |
Vector:[Geeks, for, Geeks, 10, 20] Trying to replace 10th element with GFG java.lang.ArrayIndexOutOfBoundsException: 10 >= 5
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Stack setElementAt() method in Java with Example
- vector :: cbegin() and vector :: cend() in C++ STL
- Vector removeElement() method in Java with Example
- Vector removeRange() method in Java with Example
- Vector toString() method in Java with Example
- Vector setSize() method in Java with Example
- Vector trimToSize() method in Java with Example
- Vector removeAllElements() method in Java with Example
- Vector ensureCapacity() method in Java with Example
- Vector clear() Method in Java
- Vector addAll() Method in Java
- Vector set() Method in Java
- Vector isEmpty() Method in Java
- Vector lastIndexOf() Method in Java
- Vector addElement() Method in Java
- Vector subList() Method in Java
- Vector add() Method in Java
- Vector capacity() Method in Java
- Vector contains() Method in Java
- Vector containsAll() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

