The java.util.vector.indexOf(Object element) method is used to check and find the occurrence of a particular element in the vector. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the vector does not contain the element.
Parameters: This method accepts a mandatory parameter element of the type of Vector. It specifies the element whose occurrence is needed to be checked in the Vector.
Return Value: This method returns the index or position of the first occurrence of the element in the vector. Else it returns -1 if the element is not present in the vector. The returned value is of integer type.
Below programs illustrate the Java.util.Vector.indexOf() method:
Vector: [Geeks, for, Geeks, 10, 20] The first occurrence of Geeks is at index:0 The first occurrence of 10 is at index: 3
Vector: [1, 2, 3, 10, 20] The first occurrence of Geeks is at index:1 The first occurrence of 10 is at index: 4
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.