Java Vector elementAt() Method

Java Vector Java Vector


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:

  1. When you need to randomly accessVectora specific element in
  2. Access collection elements in an environment that requires thread safety
  3. When your code needs to interact with legacy systems (becauseVectorit is a collection class in early versions of Java)

Basic Usage Example

Below is a simple example showing how to useelementAt()method:

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 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

// These two ways are equivalent
String fruit1 = fruits.elementAt(1);
String fruit2 = fruits.get(1);

Main Differences:

  1. elementAt()YesVectorspecific method, whereasget()YesListmethod defined by the interface
  2. Functionally, the two are exactly the same
  3. In modern Java programming, it is recommended to useget()method, because it conforms to the unified interface of the collection framework

Exception Handling

UsingelementAt()method, you should pay attention to handling possibleArrayIndexOutOfBoundsException:

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 + " out of range!");
}

Performance Considerations

  1. elementAt()The method has a time complexity of O(1) because it can directly access elements by index
  2. SinceVectoris synchronized,elementAt()the method is also synchronized, which provides thread safety in multi-threaded environments, but introduces unnecessary performance overhead in single-threaded environments
  3. If used in a single-threaded environment, consider usingArrayListofget()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.

Java Vector Java Vector

Other Extensions