Java Vector elementAt() Method
elementAt()The method is, in Java,Vectora member method provided by the class, used to obtain the vector(Vector) at the specified index position. 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 be returned (starting from 0)
Return Value:
- Returns the element at the specified index position
Exception:
- Throws if the index is out of range (index < 0 || index >= size())
ArrayIndexOutOfBoundsException
Usage Scenarios
elementAt()The method is usually used in the following cases:
- When you need to randomly access
Vectora specific element in - Access collection elements in an environment that requires thread safety
- When your code needs to interact with legacy systems (because
Vectorit is a collection class in early versions of Java)
Basic Usage Example
Below is a simple example showing how to useelementAt()method:
Example
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 the 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 get() Method
VectorThe class also providesget()method, which iselementAt()similar in functionality to the method:
Example
String fruit1 = fruits.elementAt(1);
String fruit2 = fruits.get(1);
Main Differences:
elementAt()YesVectorspecific method, whereasget()YesListmethod defined by the interface- Functionally, the two are exactly the same
- In modern Java programming, it is recommended to use
get()method, because it conforms to the unified interface of the collection framework
Exception Handling
UsingelementAt()method, you should pay attention to handling possibleArrayIndexOutOfBoundsException:
Example
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
if (index >= 0 && index < fruits.size()) {
String fruit = fruits.elementAt(index);
System.out.println(fruit);
} else {
System.out.println("Index " + index + " out of range!");
}
Performance Considerations
elementAt()The method has a time complexity of O(1) because it can directly access elements by index- Since
Vectoris synchronized,elementAt()the method is also synchronized, which provides thread safety in multi-threaded environments, but introduces unnecessary performance overhead in single-threaded environments - If used in a single-threaded environment, consider using
ArrayListofget()method, which has higher performance
Summary
elementAt()The method isVectora basic operation provided by the class to access elements by index. Although it is functionally equivalent toget()method, but in modern Java development, it is recommended to useArrayListandget()method combination, unless you really needVectorthe thread safety features of.
Remember, when using any index access method, make sure the index value is within a valid range to avoid runtime exceptions.
Other Extensions
Java Vector