Vector indexOf(Object) in Java

indexOf(Object): This method is available in java.util.Vector class of Java.

Syntax:

int java.util.Vector.indexOf(Object o)

This method takes one argument. This method returns the index of the first occurrence of the specified element in this vector, or -1 if this vector does not contain the element.

Parameters: One parameter is required for this method.

o: the element to search for.

Returns: the index of the first occurrence of the specified element in this vector, or -1 if this vector does not contain the element.

Exceptions: NA

Approach

Java

import java.util.Vector;

public class VectorindexOf {
    public static void main(String[] args) {

        Vector<String> vector = new Vector<>();

        vector.add("Hello");
        vector.add("Java");
        vector.add("C++");
        vector.add("Program");

        Object o = "Java";

        System.out.println(vector.indexOf(o));
    }
}

Output:

1


No comments:

Post a Comment