Java Vector Indexof
[ Java Vector](#)
* * *
`indexOf(Object elem)` is a commonly used method provided by the `Vector` class in Java, used to find the first occurrence position of a 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 Type | Description |
| --- | --- |
| int | Returns the index position of the first occurrence of the specified element, or -1 if the vector does not contain the element |
* * *
## Method Characteristics
### Linear Search
The `indexOf()` method performs a linear search starting from the first element of the vector, proceeding sequentially backward until it finds a matching element or traverses the entire vector.
### Element Comparison
This method uses the `equals()` method to determine if elements are equal. Therefore, for custom objects, you need to properly override the `equals()` method to get 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 fruits =new Vector();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Banana");// Duplicate element
// Find element position
int index1 = fruits.indexOf("Banana");
System.out.println("First occurrence of Banana: "+ index1);// Output 1
int index2 = fruits.indexOf("Grape");
System.out.println("Position of Grape: "+ index2);// Output -1
}
}
### Custom Object Search
## 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 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 people =new Vector();
people.add(new Person("Alice", 25));
people.add(new Person("Bob", 30));
// Find Person object
Person target =new Person("Bob", 30);
int index = people.indexOf(target);
System.out.println("Position of Bob: "+ index);// Output 1
}
}
* * *
## Notes
1. **Null Value Handling**: `Vector` can contain null elements, and you can use `indexOf(null)` to find the position of null elements.
2. **Performance Considerations**: For large vectors, frequent use of `indexOf()` may cause performance issues because it needs to traverse the entire vector.
3. **Thread Safety**: `Vector` is thread-safe, and the `indexOf()` method can be safely used in multi-threaded environments.
4. **Difference from ArrayList**: `ArrayList` also has a similar `indexOf()` method, but `Vector`'s method is synchronized while `ArrayList`'s is not.
* * *
## Related Methods
| Method | Description |
| --- | --- |
| `lastIndexOf(Object elem)` | Returns the last occurrence position of the specified element |
| `contains(Object elem)` | Checks if 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 elements in a vector. Understanding its working principles and characteristics is very important for effectively using the `Vector` collection. Remember to properly handle the `equals()` method for custom objects, and be aware of performance impacts with large data volumes.
[ Java Vector](#)
YouTip