Java Vector elementAt() Method
elementAt() The method is in Java's Vector A member method provided by the class, used to obtain the element at the specified index position in the vector (Vector). This method is similar to array index access but provides more safety and flexibility.
Method Syntax:
public synchronized E elementAt(int index)
Parameters:
index- The index position of the element to return (starting from 0)
Return Value:
- Returns the element at the specified index position
exception:
- If the index is out of range (index < 0 || index >= size()), a
ArrayIndexOutOfBoundsException
Use Cases
elementAt() The method is usually used in the following situations:
- When you need random access to
Vectorspecific elements in the - When accessing collection elements in an environment that requires thread safety
- When your code needs to interact with legacy systems (because
Vectoris a collection class from early Java versions)
Basic Usage Example
Here is a simple example showing how to use elementAt() method in the component:
Example
import
java.util.Vector
;
public
class
VectorExample
{
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
(
"Cherry"
)
;
// Use elementAt() to get an element
String
secondFruit
=
fruits.
elementAt
(
1
)
;
System
.
out
.
println
(
"The second fruit is: "
secondFruit
)
;
// Iterate over the elements in the Vector
for
(
int
i
=
0
;
i
<
fruits.
size
(
)
;
i
++
)
{
System
.
out
.
println
(
"Index "
i
" fruit: "
fruits.
elementAt
(
i
)
)
;
}
}
}
Output result:
第二个水果是: Banana
索引 0 的水果: Apple
索引 1 的水果: Banana
索引 2 的水果: Cherry
Comparison with the get() Method
Vector The class also provides get() method, which differs from elementAt() methods are similar in function:
Example
// These two approaches are equivalent
String
fruit1
=
fruits.
elementAt
(
1
)
;
String
fruit2
=
fruits.
get
(
1
)
;
Main Differences:
elementAt()IsVectoris a method specific to, whileget()IsListis a method defined by the interface- Functionally, the two are completely identical
- In modern Java programming, it is more recommended to use the
get()method because it conforms to the unified interface of the collections framework
Exception handling
Use elementAt() method, you should pay attention to handling possible ArrayIndexOutOfBoundsException:
Example
try
{
String
fruit
=
fruits.
elementAt
(
5
)
;
// Assume the Vector has only 3 elements
System
.
out
.
println
(
fruit
)
;
}
catch
(
ArrayIndexOutOfBoundsException
e
)
{
System
.
out
.
println
(
"Error: index out of range!"
)
;
}
A better approach is to check the index before accessing:
Example
int
index
=
5
;
if
(
index
=
0
&&
index
<
fruits.
size
(
)
)
{
String
fruit
=
fruits.
elementAt
(
index
)
;
System
.
out
.
println
(
fruit
)
;
}
else
{
System
.
out
.
println
(
"Index "
index
" is out of range!"
)
;
}
Performance considerations
elementAt()The method's time complexity is O(1) because it can access elements directly by index- Because
Vectoris synchronized, and theelementAt()method is also synchronized, which provides thread safety in a multithreaded environment but brings unnecessary performance overhead in a single-threaded environment - If used in a single-threaded environment, consider using the
ArrayList'sget()method, which has higher performance
Summary
elementAt() The method is Vector A basic operation provided by the class, used to access elements by index. Although it is functionally the same as the get() method, in modern Java development it is more recommended to use the ArrayList and get() method combination, unless you really need Vector 's thread safety features.
Remember, when using any index access method, ensure the index value is within the valid range to avoid runtime exceptions.
Java Vector