Vector indexOf() Method in Java Last Updated : 08 Mar, 2023 Comments Improve Suggest changes Like Article Like Report 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. Syntax: Vector.indexOf(Object 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. Example 1: Java // Java Program to illustrate indexOf() Method // of Vector class // Importing required classes import java.util.*; // Main class // VectorDemo public class GFG { // Main driver method public static void main(String args[]) { // Creating an empty Vector by creating object of // Vector class of string type Vector<String> vec_tor = new Vector<String>(); // Adding elements in the Vector // using add() method vec_tor.add("Geeks"); vec_tor.add("for"); vec_tor.add("Geeks"); vec_tor.add("10"); vec_tor.add("20"); // Print and display all elements in above vector // object System.out.println("Vector: " + vec_tor); // Print commands where we are returning the 1st // position of element in vector object using // indexOf() method System.out.println( "The first occurrence of Geeks is at index:" + vec_tor.indexOf("Geeks")); System.out.println( "The first occurrence of 10 is at index: " + vec_tor.indexOf("10")); } } Output: 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. Example 2: Java // Java Program to illustrate indexOf() Method // of Vector class // Importing required classes import java.util.*; // Main class // VectorDemo public class GFG { // Main driver method public static void main(String args[]) { // Creating an empty Vector Vector<Integer> vec_tor = new Vector<Integer>(); // Adding elements in the Vector // using add() method vec_tor.add(1); vec_tor.add(2); vec_tor.add(3); vec_tor.add(1); vec_tor.add(5); // Print and display all elements of vector object System.out.println("Vector: " + vec_tor); // Returning the 1st position of an element // using indexOf() method // Print and display commands System.out.println("The first occurrence of 1 is at index : "+ vec_tor.indexOf(1)); System.out.println("The first occurrence of 3 is at index : "+ vec_tor.indexOf(7)); } } OutputVector: [1, 2, 3, 1, 5] The first occurrence of 1 is at index : 0 The first occurrence of 3 is at index : -1 Time complexity: O(n), // n is the number of elements in the vector. Auxiliary space: O(n) 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. Comment More infoAdvertise with us Next Article Vector indexOf() Method in Java K kundankumarjha Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions Java-Vector +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads 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 of Vector capacity() methodVector.capacity()Parameters: The method does not take any parameter. Return Value: The method returns the capacity or the intern 2 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( 2 min read Java Vector clone() Method In Java, the clone() method of the Vector class is used to return a shallow copy of the vector. It just creates a copy of the vector. The copy will have a reference to a clone of the internal data array but not a reference to the original internal data array.Example 1: In this example, we use the cl 3 min read Java Vector contains() Method The contains() method in Java is used to check whether a specific element is present in the Vector or not.Example 1: In this example, we will check whether a particular string is present in the vector or not.Java// Java program to demonstrate the use // of the contains() method with Strings import j 3 min read Java Vector containsAll() Method In Java, the containsAll() method is used to check if the Vector contains all the elements of a specified Collection or not.Example 1: Below program demonstrating the use of containsAll() method with Strings.Java// Java Program to demonstrate the use of // containsAll() with Strings import java.util 2 min read Java Vector copyInto() Method In Java, the copyInto() method is used to copy all the components from one vector to another array, having enough space to hold all of the components of the vector. It is to be noted that the index of the elements remains unchanged. The elements present in the array are replaced by the elements of t 3 min read Java Vector elementAt() Method In Java, the elementAt() method is used to fetch or retrieve an element at a specific index from a Vector. It allows to access elements in the vector by providing the index, which is zero-based.Example 1: Here, we use the elementAt() method to retrieve an element at a specified index with an Integer 2 min read Java Vector elements() Method In Java, the elements() method is used to return an enumeration of the elements in the Vector. This method allows you to iterate over the elements of a Vector using an Enumeration, which provides methods for checking if there are more elements and retrieving the next element.Example 1: Below is the 2 min read Vector ensureCapacity() method in Java with Example The ensureCapacity() method of Java.util.Vector class increases the capacity of this Vector instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. Syntax: public void ensureCapacity(int minCapacity) Parameters: This method takes 2 min read Vector equals() Method in Java The java.util.vector.equals(Object obj) method of Vector class in Java is used verify the equality of an Object with a vector and compare them. The list returns true only if both Vector contains same elements with same order. Syntax: first_vector.equals(second_vector) Parameters: This method accepts 2 min read Like