Java Vector setElementAt() Method

Java Vector Java Vector


setElementAt()is a method in JavaVectorprovided by the Vector class, used to modify the element at the specified position in the vector. This method allows you to directly update a specific element in the vector without having to first delete the old element and then insert a new one.

Method Description:

public void setElementAt(E obj, int index)

Parameter Description

obj

  • Type: E (generic, consistent with the type defined by Vector)
  • Description: The new element to set
  • Example: Can be any object, such as String, Integer, etc.

index

  • Type: int
  • Description: The position (index) of the element to modify
  • Note: Index starts counting from 0

Return Value

This method has no return value (void type); it simply modifies the element at the specified position in the vector.


Usage Example

Example

import java.util.Vector;

public class VectorExample {
    public static void main(String[] args) {
        // Create a Vector containing strings
        Vector<String> fruits = new Vector<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");
       
        System.out.println("Vector before modification: " + fruits);
       
        // Use setElementAt() to modify the second element
        fruits.setElementAt("Grape", 1);
       
        System.out.println("Vector after modification: " + fruits);
    }
}

Output Result:

修改前的 Vector: [Apple, Banana, Orange]
修改后的 Vector: [Apple, Grape, Orange]

Notes

1. Index Out of Bounds Exception

If the specified index parameter exceeds the valid range of the Vector (less than 0 or greater than or equal to size()), an ArrayIndexOutOfBoundsException will be thrown.

Incorrect Example:

Example

Vector<Integer> numbers = new Vector<>();
numbers.add(10);
numbers.setElementAt(20, 1);  // Throws ArrayIndexOutOfBoundsException

2. Thread Safety

Vector is thread-safe, and the setElementAt() method is also synchronized, so it can be safely used in a multi-threaded environment.

3. Difference from the set() Method

  • setElementAt()is a method unique to Vector
  • set()The set() method comes from the List interface; its functionality is the same, but it returns the replaced old element.
  • It is recommended to use in new codeset()the set() method to maintain consistency with the List interface.

Practical Application Scenarios

Scenario 1: Updating Data Collections

When you need to modify a specific element in a collection based on a condition, you can use this method.

Example

Vector<Student> students = new Vector<>();
// ... Add student objects

// Find the student to modify and update the grade
students.setElementAt(new Student("John", "A"), 2);

Scenario 2: Game Development

In game development, it can be used to update the state of game objects.

Example

Vector<GameObject> objects = new Vector<>();
// ... Initialize game objects

// Update the game object at a specific position
objects.setElementAt(new PowerUp(), 3);

Summary

setElementAt()The setElementAt() method is an important method in the Vector class for modifying elements; it provides the ability to directly modify an element at a specified position. Although it is recommended to use ArrayList and the set() method, which implement the List interface, in new code, understanding this method is still valuable when thread safety is needed or when maintaining legacy code.

Remember to pay attention to the index range when using it, avoid out-of-bounds exceptions, and choose the appropriate collection type and method according to actual needs.

Java Vector Java Vector

Other Extensions