The Wayback Machine - https://web.archive.org/web/20240618020141/https://www.geeksforgeeks.org/vector-set-method-in-java/
Open In App

Vector set() Method in Java

Last Updated : 24 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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: 

Java




// 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);
    }
}


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: 

Java




// 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);
    }
}


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).



Previous Article
Next Article

Similar Reads

Copy Elements of One Java Vector to Another Vector in Java
Vector is similar to arrays but is growable also, or we can say no fixed size is required. Previously vector was a part of legacy classes but now it is part of Collections. It also implements a List interface, so we can use any method of list interface on vectors also. Syntax : Vector&lt;Integer&gt; gfg=new Vector&lt;&gt;(); Ways To copy elements o
3 min read
Vector clear() Method in Java
The Java.util.Vector.clear() method is used to remove all the elements from a Vector. Using the clear() method only clears all the element from the vector and does not delete the vector. In other words, we can say that the clear() method is used to only empty an existing vector. Syntax: Vector.clear() Parameters: The method does not take any parame
2 min read
Vector addAll() Method in Java
java.util.Vector.addAll(Collection C): This method is used to append all of the elements from the collection passed as a parameter to this function to the end of a vector keeping in mind the order of return by the collection's iterator. Syntax: boolean addAll(Collection C) Parameters: The method accepts a mandatory parameter C which is a collection
2 min read
Vector isEmpty() Method in Java
The Java.util.Vector.isEmpty() method in Java is used to check and verify if a Vector is empty or not. It returns True if the Vector is empty else it returns False. Syntax: Vector.isEmpty() Parameters: This method does not take any parameter. Return Value: This function returns True if the Vectoris empty else it returns False. Below programs illust
2 min read
Vector lastIndexOf() Method in Java
The Java.util.Vector.lastIndexOf(Object element) method is used to check and find the occurrence of a particular element in the vector. If the element is present in the vector then the lastIndexOf() method returns the index of last occurrence of the element otherwise it returns -1. This method is used to find the last occurrence of a particular ele
2 min read
Vector addElement() Method in Java
The Java.util.Vector.addElement() method is used to append a specified element to the end of this vector by increasing the size of the vector by 1. The functionality of this method is similar to that of the add() method of Vector class.Syntax: boolean addElement(Object element) Parameters: This function accepts a single parameter element of object
2 min read
Vector subList() Method in Java
The java.util.Vector.subList() is used to return a sublist of the existing Vector within a range mentioned in the parameter. The method takes in an upper limit and a lower limit and returns all the elements mentioned in the range. The lower limit is included if the element is present in the list and the upper limit is excluded. Basically, it takes
3 min read
Vector add() Method in Java
boolean add(Object element): This method appends the specified element to the end of this vector. Syntax: boolean add(Object element) Parameters: This function accepts a single parameter element as shown in the above syntax. The element specified by this parameter is appended to end of the vector. Return Value: This method returns True after succes
2 min read
Vector capacity() Method in Java
The Java.util.Vector.capacity() method in Java is used to get the capacity of the Vector or the length of the array present in the Vector. Syntax: Vector.capacity() Parameters: The method does not take any parameter. Return Value: The method returns the capacity or the internal data array’s length present in the Vector, which is an integer value. B
2 min read
Vector contains() Method in Java
The java.util.vector.contains() method is used to check whether a specific element is present in the Vector or not. So basically it is used to check if a vector contains any particular element or not. Syntax: Vector.contains(Object element) Parameters: This method takes a mandatory parameter element which is of the type of vector. This is the eleme
2 min read