VOOZH about

URL: https://www.geeksforgeeks.org/java/vector-set-method-in-java/

⇱ Vector set() Method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Vector set() Method in Java

Last Updated : 11 Jul, 2025

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: 

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: 

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]

Time complexity: O(n). // n is the size of the vector.
Auxiliary Space: O(n).

Comment