Vector indexOf() Method in Java
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: Element of the type of Vector which is mandatory as it specifies the element whose occurrence is needed to be checked in the Vector.
Return Value: 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.
Vector: [Geeks, for, Geeks, 10, 20] The first occurrence of Geeks is at index:0 The first occurrence of 10 is at index: 3
Output Explanation: Here we have inserted elements inside the vector in which few elements were duplicated. In the above example “Geeks” is the only element inside Vector class being getting repeated so do we returned the first occurrence which is present at index so do we returned ‘0’ while if the element is not getting duplicated then simply its index will be returned.
Vector: [1, 2, 3, 1, 5] The first occurrence of 1 is at index : 0 The first occurrence of 3 is at index : -1
Output Explanation: As we did above here we take integers in the case of strings where the here only thing that we plot differently is bypassing an element that does not exists then ‘-1’ will be returned because there do not exist any negative indexes in java so do we assign -1 in general.