Replace an element at specified index of Java Vector Example
- /*
- Replace an element at specified index of Java Vector Example
- This Java Example shows how to replace an element at specified index of java
- Vector object using set method.
- */
- import java.util.Vector;
- public class ReplaceElementAtSpecifiedIndexVectorExample {
- public static void main(String[] args) {
- //create a Vector object
- Vector v = new Vector();
- //Add elements to Vector
- v.add("1");
- v.add("2");
- v.add("3");
- /*
- To replace an element at the specified index of Vector use
- Object set(int index, Object obj) method.
- This method replaces the specified element at the specified index in the
- Vector and returns the element previously at the specified position.
- */
- v.set(1,"REPLACED ELEMENT");
- System.out.println("Vector contains...");
- //display elements of Vector
- for(int index=0; index < v.size(); index++)
- System.out.println(v.get(index));
- }
- }
- /*
- Output would be
- Vector contains...
- 1
- REPLACED ELEMENT
- 3
- */



