Changing the Value of an Element in Vector in Java
This example shows how to modify or replace elements in Vector using the set(int index, Object o) of the Vector.public E set(int index, Object o): replaces the element of the specific position in Vector with the new element o.
import java.util.Vector;Runtime:
public class replaceElementVector {
public static void main(String[] args) {
Vector< String> vct = new Vector< String> ();
//add elements
vct.add("e1");
vct.add("e2");
vct.add("e3");
vct.add("e4");
System.out.println("Before:");
for(String e:vct)
System.out.println(e);
//replace the value of the element at index 2 with e31
vct.set(2, "e31");
System.out.println("\nAfter updated:");
for(String e:vct)
System.out.println(e);
}
}
Before:
e1
e2
e3
e4
After updated:
e1
e2
e31
e4