How to Replace an Element at a Specific Index of the Vector in Java?
Last Updated :
19 Jan, 2021
The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in the java.util package and implements the List interface, so we can use all the methods of List interface here.
Examples Input : Vector= ["Harry","Steve","Vince","David","Matt"],Index=1,Element ="Mark"
Output : Vector= ["Harry","Mark","Vince","David","Matt"]
Input : Vector= ["Harry","Steve","Vince","David","Matt"],Index=2,Element ="Jack"
Output : Vector= ["Harry","Mark","Jack","David","Matt"]
Approach:
An element in the vector can be replaced at a particular/specified index with another element using set() method, or we can say java.util.Vector.set() method.
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.
Java
// Java Program to replace a element at a particular index
// in vector
import java.util.Vector;
public class GFG {
public static void main(String[] args)
{
// making a new vector object
Vector<String> vector = new Vector<String>();
// adding elements to the vector.
vector.add("Harry");
vector.add("Steve");
vector.add("Vince");
vector.add("David");
vector.add("Matt");
System.out.println("Vector elements before replacement: ");
for (int i = 0; i < vector.size(); i++)
{
System.out.print(vector.get(i) + " ");
}
System.out.println();
// Replacing index 1 element
vector.set(1, "Mark");
// Replacing index 2 element
vector.set(2, "Jack");
System.out.println("Vector elements after replacement: ");
for (int i = 0; i < vector.size(); i++)
{
System.out.print(vector.get(i) + " ");
}
System.out.println();
}
}
OutputVector elements before replacement:
Harry Steve Vince David Matt
Vector elements after replacement:
Harry Mark Jack David Matt
Similar Reads
How to Iterate the Vector Elements in the Reverse Order in Java? The Vector class is found in java.util package and it implements List interface. The Vector class is included in the java collection framework from Java version 1.2. Unlike arrays, vectors can grow and shrink their size, and thus they are also called the Dynamic Array. Vectors are synchronized, ie t
3 min read
Java Program to Search an Element in Vector A vector in Java is a dynamic array that can be resized as needed. It is synchronized, which means it is safe in multi-threaded programs. To find an element in a Vector we have to loop through its elements to find a match. In this article, we will learn how to search for a component in a Vector usin
3 min read
How to Replace a Element in Java ArrayList? To replace an element in Java ArrayList, set() method of java.util. An ArrayList class can be used. The set() method takes two parameters the indexes of the element that has to be replaced and the new element. The index of an ArrayList is zero-based. So, to replace the first element, 0 should be the
2 min read
How to Get First and Last Element From the Vector in Java? The first element in the Vector can be obtained using the firstElement() method of vector class that is represent util package. The last element in the Vector can be obtained using the lastElement() method of vector class that is also represent util package. Neither of these methods requires any par
2 min read
How to Swap Two Elements in an ArrayList in Java? We can swap two elements of Array List using Collections.swap() method. This method accepts three arguments. The first argument is the ArrayList and the other two arguments are the indices of the elements. This method returns nothing. Syntax: public static void swap(List list, int a, int b); Parame
2 min read
Searching Elements in Vector Using Index in Java Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. An element of a Vector can be searched using an index with different methods. Suppose if the element is not present in the Vector then th
4 min read