Java Vector indexOf() Method
indexOf(Object elem) is a method in Java's Vector A commonly used method provided by the class, used to find the position where the specified element first appears in the vector. If the vector does not contain the element, returns -1.
Method Syntax
public int indexOf(Object elem)
Method parameters
| Parameter name | Parameter type | Description |
|---|---|---|
elem |
Object | The element to find in the vector |
Return Value
| Return value 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
indexOf() The method performs a linear search starting from the first element of the vector and moving forward until it finds a matching element or traverses the entire vector.
faster
This method uses the equals() method to determine whether elements are equal, so for custom objects, you need to correctly override equals() method to get the expected result.
Time Complexity
Because it is a linear search, the method's time complexity 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"
)
;
If the list contains multiple identical elements, only the index of the first match is returned
// Find the element position
int
index1
=
fruits.
indexOf
(
"Banana"
)
;
System
.
out
.
println
(
"Position of the first occurrence of Banana: "
index1
)
;
// Output 1
int
index2
=
fruits.
indexOf
(
"Grape"
)
;
System
.
out
.
println
(
"Position of Grape: "
index2
)
;
// Output -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
)
;
// Output 1
}
}
Notes
-
null Value Handling:
Vectorcan contain null elements; you can useindexOf(null)to find the position of a null element. -
Performance considerations: For large vectors, frequent use of
indexOf()may cause performance problems because it needs to traverse the entire vector. -
Thread Safety:
Vectoris thread-safe,indexOf()The method can be used safely in a multithreaded environment. -
Differences from ArrayList:
ArrayListalso has a similarindexOf()method,Vectormethod is synchronized, whileArrayList's is 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 how it works and its characteristics is very important for effectively using the Vector collection. Remember to correctly handle custom objects' equals() method, and note the performance impact with large amounts of data.
Java Vector