In this example, we may see how to replace an element at specified index Vector object with set method.
Source Code
package com.beginner.examples;
import java.util.Vector;
public class ReplaceElementAtSpecifiedIndexVector {
public static void main(String[] args) {
//create Vector.
Vector vector = new Vector();
vector.add("A");
vector.add("B");
vector.add("C");
vector.add("D");
System.out.println("Before element replacement ,Vector contains : ");
for(int index=0; index < vector.size(); index++) {
System.out.println(vector.get(index));
}
/*
* To replace an element at the specified index of Vector use set method.
*/
vector.set(2,"X");
System.out.println("After element replacement ,Vector contains : ");
for(int index=0; index < vector.size(); index++) {
System.out.println(vector.get(index));
}
}
}
Output:
Before element replacement ,Vector contains :
A
B
C
D
After element replacement ,Vector contains :
A
B
X
D
References
Imported packages in Java documentation: