How to replace element of vector

 We can replace any specific index element by other element using set(int index,element) method.

import java.util.Vector;

public class ReplaceElement {

public static void main(String[] args) {

Vector<String> vector = new Vector<String>();
//add element in vector
vector.add(“cricket”);
vector.add(“hockey”);
vector.add(“football”);
vector.add(“tennish”);

// print vector
System.out.println(“Before replace Vector is = “+vector);

/*
* Replaces the element at the specified
* position in this Vector with the
*  specified element.
*/
vector.set(1,”Apple”);

System.out.println(“After Replace element vector is = “+vector);

}

}

Output:

Before replace Vector is = [cricket, hockey, football, tennish]

After Replace element vector is = [cricket, Apple, football, tennish]

Leave a Reply

Your email address will not be published. Required fields are marked *

− 1 = 9