Java Vector setElementAt() Method
setElementAt() is a method in Java's Vector A method provided by the class, used to modify the element at the specified position in the vector (Vector). This method allows you to directly update a specific element in the vector without first deleting the old element and then inserting 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: The index starts counting from 0
Return Value
This method has no return value (void type); it only modifies the element at the specified position in the vector.
Usage examples
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 argument is outside the valid range of the Vector (less than 0 or greater than or equal to size()), an ArrayIndexOutOfBoundsException is 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 used safely in a multithreaded environment.
3. Differences from the set() Method
setElementAt()is a Vector-specific methodset()method comes from the List interface, has the same function but returns the old element that was replaced- It is recommended to use
set()method in new code to maintain consistency with the List interface
Practical Application Scenarios
Scenario 1: Updating a Data Collection
When you need to modify specific elements in a collection based on conditions, you can use this method.
Example
Vector
<
Student
students
=
new
Vector
<>
(
)
;
// ... add student objects
// Find the student to modify and update the score
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() method is an important method in the Vector class for modifying elements; it provides the ability to directly modify the element at a specified position. Although in new code it is recommended to use ArrayList, which implements the List interface, and the set() method, understanding this method is still valuable when thread safety is needed or old code is maintained.
Remember to pay attention to the index range when using it to avoid out-of-bounds exceptions, and choose the appropriate collection type and method based on actual needs.
Java Vector