The Wayback Machine - https://web.archive.org/web/20240618020143/https://www.geeksforgeeks.org/vector-indexof-method-in-java/
Open In App

Vector indexOf() Method in Java

Last Updated : 08 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
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));
    }
}


Output

Vector: [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.  



Previous Article
Next Article

Similar Reads

Java.util.Arraylist.indexOf() in Java
The indexOf() method of ArrayList returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. Syntax : public int IndexOf(Object o) obj : The element to search for. // Java code to demonstrate the working of // indexOf in ArrayList // for ArrayList functions import java.util.Arra
2 min read
Java.util.LinkedList.indexOf(), lastIndexof() in Java
Linked list library also offers to depict the first and last index of element that has to be found using the indexOf() and lastIndexOf() functions respectively. They offer a variety as the direct access is not available in the conventionally made linked list, hence knowledge of it is useful. 1. indexOf(Object o) : This method returns the index of t
4 min read
LinkedList indexOf() method in Java
The Java.util.LinkedList.indexOf(Object element) method is used to check and find the occurrence of a particular element in the list. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the list does not contain the element. Syntax: LinkedList.indexOf(Object element) Parameters: Th
2 min read
CopyOnWriteArrayList indexOf() method in Java
The indexOf(Object o) method of CopyOnWriteArrayList returns the first occurrence of the element passed in the list. It returns -1 if the element is not present in the list. Syntax: public int indexOf(Object o) Parameters: The function accepts a parameter o whose first occurrence is to be returned. Return Value: The function returns the first occur
3 min read
Java Guava | Bytes.indexOf(byte[] array, byte target) method with Examples
Bytes.indexOf(byte[] array, byte target) method of Guava's Bytes Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(byte[] array, byte target) Parameters:
3 min read
Java Guava | Floats.indexOf(float[] array, float target) method with Examples
Floats.indexOf(float[] array, float target) method of Guava's Floats Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(float[] array, float target) Param
3 min read
Java Guava | Doubles.indexOf(double[] array, double target) method with Examples
Doubles.indexOf(double[] array, double target) method of Guava's Doubles Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(double[] array, double target)
3 min read
Java Guava | Shorts.indexOf(short[] array, short target) method with Examples
Shorts.indexOf(short[] array, short target) method of Guava's Shorts Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(short[] array, short target) Param
3 min read
Java Guava | Booleans.indexOf(boolean[] array, boolean target) method with Examples
Booleans.indexOf(boolean[] array, boolean target) method of Guava's Booleans Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(boolean[] array, boolean t
3 min read
AbstractList indexOf() method in Java with Examples
The indexOf() method of java.util.AbstractList class is used to return the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index. Syntax: public int indexOf(
3 min read