Vector set() Method in Java

The Java.util.Vector.set() method is used to replace any particular element in the vector, created using the Vector class, with another element.

Syntax:

Vector.set(int index, Object element)

Parameters: This function accepts two mandatory parameters as shown in the above syntax and described below.

  • index: This is of integer type and refers to the position of the element that is to be replaced from the vector.
  • element: It is the new element by which the existing element will be replaced and is of the same object type as the vector.

Return Value: The method returns the previous value from the vector that is replaced with the new value.

Below programs illustrate the Java.util.Vector.set() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate set()
import java.util.*;
  
public class VectorDemo {
    public static void main(String args[])
    {
        // Creating an empty Vector
        Vector<String> vec_tor = new Vector<String>();
  
        // Use add() method to add elements in the vector
        vec_tor.add("Geeks");
        vec_tor.add("for");
        vec_tor.add("Geeks");
        vec_tor.add("10");
        vec_tor.add("20");
  
        // Displaying the Vector
        System.out.println("Vector: " + vec_tor);
  
        // Using set() method to replace Geeks with GFG
        System.out.println("The Object that is replaced is: "
                           + vec_tor.set(2, "GFG"));
  
        // Using set() method to replace 20 with 50
        System.out.println("The Object that is replaced is: "
                           + vec_tor.set(4, "50"));
  
        // Displaying the modified vector
        System.out.println("The new Vector is:" + vec_tor);
    }
}

chevron_right


Output:

Vector: [Geeks, for, Geeks, 10, 20]
The Object that is replaced is: Geeks
The Object that is replaced is: 20
The new Vector is:[Geeks, for, GFG, 10, 50]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate set()
import java.util.*;
  
public class VectorDemo {
    public static void main(String args[])
    {
        // Creating an empty Vector
        Vector<Integer> vec_tor = new Vector<Integer>();
  
        // Use add() method to add elements in the vector
        vec_tor.add(12);
        vec_tor.add(23);
        vec_tor.add(22);
        vec_tor.add(10);
        vec_tor.add(20);
  
        // Displaying the Vector
        System.out.println("Vector: " + vec_tor);
  
        // Using set() method to replace 12 with 21
        System.out.println("The Object that is replaced is: "
                           + vec_tor.set(0, 21));
  
        // Using set() method to replace 20 with 50
        System.out.println("The Object that is replaced is: "
                           + vec_tor.set(4, 50));
  
        // Displaying the modified vector
        System.out.println("The new Vector is:" + vec_tor);
    }
}

chevron_right


Output:

Vector: [12, 23, 22, 10, 20]
The Object that is replaced is: 12
The Object that is replaced is: 20
The new Vector is:[21, 23, 22, 10, 50]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.