Java Vector indexOf() Method

Java Vector Java Vector


indexOf(Object elem)is in JavaVectora common method provided by the class, used to find the position of the first occurrence of the specified element in the vector. If the vector does not contain the element, it returns -1.

Method Syntax

public int indexOf(Object elem)

Method Parameters

Parameter Name Parameter Type Description
elem Object The element to be searched for in the vector

Return Value

Return Value Type Description
int Returns the index position of the first occurrence of the specified element; returns -1 if the vector does not contain the element

Method Features

Linear Search

indexOf()The method starts from the first element of the vector and performs a linear search one by one until it finds a matching element or has traversed the entire vector.

Element Comparison

This method uses theequals()method to determine whether elements are equal. Therefore, for custom objects, you need to correctly override theequals()method to obtain the expected results.

Time Complexity

Since it is a linear search, the time complexity of this method is O(n), where n is the size of the vector.


Usage Examples

Basic Usage

Example

import java.util.Vector;

public class VectorIndexOfExample {
    public static void main(String[] args) {
        // Create a Vector
        Vector<String> fruits = new Vector<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");
        fruits.add("Banana");  // Duplicate elements
       
        // Find element position
        int index1 = fruits.indexOf("Banana");
        System.out.println("Position of first occurrence of Banana: " + index1);  // Prints 1
       
        int index2 = fruits.indexOf("Grape");
        System.out.println("Position of Grape: " + index2);  // Prints -1
    }
}

Custom Object Lookup

Example

import java.util.Vector;

class Person {
    private String name;
    private int age;
   
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
   
    // Override the equals method
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Person person = (Person) obj;
        return age == person.age && name.equals(person.name);
    }
}

public class CustomObjectExample {
    public static void main(String[] args) {
        Vector<Person> people = new Vector<>();
        people.add(new Person("Alice", 25));
        people.add(new Person("Bob", 30));
       
        // Find the Person object
        Person target = new Person("Bob", 30);
        int index = people.indexOf(target);
        System.out.println("Position of Bob: " + index);  // Prints 1
    }
}

Notes

  1. Handling null values:VectorCan contain null elements; you can useindexOf(null)to find the position of null elements.

  2. Performance Considerations: For large vectors, frequent use ofindexOf()may cause performance issues because it needs to traverse the entire vector.

  3. Thread Safety:Vectoris thread-safe,indexOf()method can be safely used in a multi-threaded environment.

  4. Differences from ArrayList:ArrayListalso has a similarindexOf()method, butVectormethod is synchronized, whileArrayListis not.


Related Methods

Method Description
lastIndexOf(Object elem) Returns the position of the last occurrence of the specified element
contains(Object elem) Checks whether the vector contains the specified element
get(int index) Gets the element at the specified position

Summary

Vector.indexOf(Object elem)is a simple but practical method for finding the position of an element in a vector. Understanding its working principle and characteristics is very important for effectively using theVectorcollection. Remember to correctly handle the custom objects'equals()method, and pay attention to the performance impact when dealing with large amounts of data.

Java Vector Java Vector

Other Extensions