Open In App

Searching Elements in Vector Using Index in Java

Improve
Improve
Like Article
Like
Save
Share
Report

Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. An element of a Vector can be searched using an index with different methods. Suppose if the element is not present in the Vector then the method will return -1.

Method 1: (Using indexOf(Object o))

Declaration

public int indexOf(Object o)

Syntax:

Vector.indexOf(Object 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.

Java




// Searching Elements in Vector Using Index in Java
import java.util.Vector;
  
public class GFG {
    
    public static void main(String args[])
    {
        // initialization Of Vector
        Vector<String> details = new Vector<>(10);
  
        // Adding Elements To The
        // Vector
        details.add("Geeks For Geeks");
        details.add("Welcome");
        details.add("Bob");
        details.add("Hello");
        details.add("Geeks For Geeks");
  
        // Searching The Index Of Element
        System.out.println(
            "The index of element Geeks For Geeks in Vector is: "
            + details.indexOf("Geeks For Geeks"));
        System.out.println(
            "The index of element 2100 in Vector is: "
            + details.indexOf(2100));
    }
}


Output

The index of element Geeks For Geeks in Vector is: 0
The index of element 2100 in Vector is: -1

Method 2: (Using lastIndexOf(Object o))

Declaration

public int lastIndexOf(Object o)

Syntax:

Vector.lastIndexOf(Object element)

Parameters: The parameter element is of type Vector. It refers to the element whose last occurrence is required to be checked.

Return Value: The method returns the position of the last occurrence of the element in the Vector. If the element is not present in the Vector then the method returns -1. The returned value is of integer type.

Java




// Searching Elements in Vector
// Using Index in Java
import java.util.Vector;
  
public class GFG {
    
    public static void main(String args[])
    {
        // initialization Of Vector
        Vector<String> details = new Vector<>(10);
  
        // Adding Elements To The
        // Vector
        details.add("Geeks For Geeks");
        details.add("Welcome");
        details.add("Bob");
        details.add("Hello");        
        details.add("Geeks For Geeks");
  
        // print an element using it's index
        System.out.println(
            "The index of element Geeks For Geeks in Vector is: "
            + details.lastIndexOf("Geeks For Geeks"));
    }
}


Output

The index of element Geeks For Geeks in Vector is: 4

Method 3: (Using indexOf(Object, int) method)

Declaration

public int indexOf(Object o, int Starting_Index)

Syntax

Vector.indexOf(Object, int)

Parameters:

  • starting_index: It is an index where to start searching for an element in the forward direction.
  • o: It is an element to search for.

Return Value: The Index Of the first occurrence Of the Object(o) In this Vector, searching from the given starting_Index

Java




// Searching Elements in Vector Using Index in Java
import java.util.Vector;
  
public class GFG {
    
    public static void main(String args[])
    {
        // initialization Of Vector
        Vector<String> details = new Vector<>(10);
  
        // Adding Elements To The
        // Vector
        details.add("Geeks For Geeks");
        details.add("Welcome");
        details.add("Bob");
        details.add("Hello");
        details.add("Bob");
        details.add("Geeks For Geeks");
  
        System.out.println(
            "The index of element Bob in Vector is: "
            + details.indexOf("Bob", 3));
    }
}


Output

The index of element Bob in Vector is: 4

Method 4: (Using lastIndexOf(Object, int) method)

Declaration

public int lastIndexOf(Object o, int Starting_Index)

Syntax

Vector.lastIndexOf(Object, int)

Parameters:

  • starting_index: It is an index where to start searching for an element in the backward direction.
  • o: It is an element to search for.

Return Value: The Index of the last occurrence of the specified element In this Vector, searching backward from Starting_Index.

Java




// Searching Elements in Vector Using Index in Java
import java.util.Vector;
  
public class GFG {
    
    public static void main(String args[])
    {
        // create a instance of vector
        Vector<String> details = new Vector<>(10);
  
        // Adding Elements To The
        // Vector
        details.add("Geeks For Geeks");
        details.add("Welcome");
        details.add("Bob");
        details.add("Hello");
        details.add("Bob");
        details.add("Geeks For Geeks");
  
        System.out.println(
            "The index of element Bob in Vector is: "
            + details.lastIndexOf("Bob", 4));
    }
}


Output

The index of element Bob in Vector is: 4


Last Updated : 22 Dec, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads