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.
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"]
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.
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.
Vector elements before replacement: Harry Steve Vince David Matt Vector elements after replacement: Harry Mark Jack David Matt
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.