Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Vector indexOf(Object elem, int index) Method

Java Vector indexOf(Object elem, int index) Method

Java Vector Java Vector


indexOf(Object elem, int index) is a method in Java's Vector A very useful method provided by the class, used in the Vector collection to search for a specific element starting from a specified position and return the index position of its first occurrence.

Method Syntax



public int indexOf(Object elem, int index)

Parameter Description

Parameter name Type Description
elem Object The element to find in the Vector
index int The starting index position for the search (inclusive), which must be non-negative and less than the Vector size

Return Value

Return Value Description
int Returns the index position of the first occurrence of the element, or -1 if not found

Method Characteristics

Search Range

The method starts from the specified index position and searches until the end of the Vector.

faster

Use equals() method for element comparison, so ensure the element class being searched correctly implements equals() method.

Thread Safety

Because Vector is a thread-safe collection class, and theindexOf() method is also thread-safe.

Performance considerations

This is a linear search method with a time complexity of O(n), not suitable for frequent searches on large-scale data.


Usage examples

Basic Usage Example

Example

import

java.util.Vector

;

public

class

VectorIndexOfExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a Vector and add elements

        Vector

<

String

fruits

=

new

Vector

<>

(

)

;

        fruits.

add

(

"Apple"

)

;

        fruits.

add

(

"Banana"

)

;

        fruits.

add

(

"Orange"

)

;

        fruits.

add

(

"Apple"

)

;

        fruits.

add

(

"Grape"

)

;

       

       

// Search for "Apple" starting from index 1

       

int

index

=

fruits.

indexOf

(

"Apple"

,

1

)

;

       

System

.

out

.

println

(

"First occurrence of 'Apple' starting from index 1: "

index

)

;

// Output 3

       

       

// Find a non-existent element

       

int

notFound

=

fruits.

indexOf

(

"Mango"

,

0

)

;

       

System

.

out

.

println

(

"Search result for 'Mango': "

notFound

)

;

// Output -1

   

}

}

Boundary Case Examples

Example

// Test boundary cases

Vector

<

Integer

numbers

=

new

Vector

<>

(

)

;

numbers.

add

(

10

)

;

numbers.

add

(

20

)

;

numbers.

add

(

30

)

;

numbers.

add

(

20

)

;

numbers.

add

(

40

)

;

// Start index equals the Vector size

int

result1

=

numbers.

indexOf

(

20

, numbers.

size

(

)

)

;

System

.

out

.

println

(

result1

)

;

// Output -1

// Start index 0 is equivalent to indexOf(Object elem)

int

result2

=

numbers.

indexOf

(

20

,

0

)

;

System

.

out

.

println

(

result2

)

;

// Output 1

// Find a null element

numbers.

add

(

null

)

;

int

result3

=

numbers.

indexOf

(

null

,

3

)

;

System

.

out

.

println

(

result3

)

;

// Output 5


Exception cases

IndexOutOfBoundsException

If the specified index argument is negative or greater than or equal to the Vector size, a IndexOutOfBoundsException.

Example

try

{

   

int

invalidIndex

=

fruits.

indexOf

(

"Apple"

,

1

)

;

// Throws IndexOutOfBoundsException

}

catch

(

IndexOutOfBoundsException

e

)

{

   

System

.

out

.

println

(

"Error: "

e.

getMessage

(

)

)

;

}


Comparison with Related Methods

indexOf(Object elem)

This is indexOf method's simplified version, which starts searching from index 0 by default.

lastIndexOf(Object elem)

Searches for elements backward starting from the end of the Vector.

lastIndexOf(Object elem, int index)

Searches for elements forward from the specified position.


Practical Application Scenarios

Finding Duplicate Elements

Especially useful when you need to find the second or subsequent occurrence of an element in the Vector.

Segmented Search

In large Vectors, segmented search can be used to improve efficiency.

Skipping Processed Parts

When processing Vector elements, you can skip the processed parts and continue searching.


Best Practice Recommendations

  1. Check the return value: Always check whether the returned index is -1, indicating the element was not found
  2. Parameter Validation: Ensure the passed-in index argument is within the valid range
  3. Performance considerations: For frequent search operations, consider using other data structures such as HashSet
  4. : If the list is modified during iteration, it may throw: Clarify whether null elements need to be handled, because Vector allows storing null values

Summary

Vector.indexOf(Object elem, int index) method is a practical search tool that extends the basic indexOf functionality by allowing the search to start from a specified position. Understanding how this method works and its boundary cases can help developers more effectively handle element search tasks in Vector collections.

Java Vector Java Vector