Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Vector set() Method

Java Vector set() Method

Java Vector Java Vector


Vector.set() The method is in Java's Vector A commonly used method provided by the class, used to replace the element at the specified position. This method allows you to modify Vector the value at a specific index and return the old element that was replaced.

Method Syntax


public E set(int index, E element)

Parameter Description

Return Value


Usage examples

Basic Usage

Example

import

java.util.Vector

;

public

class

VectorSetExample

{

   

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"

)

;

       

       

System

.

out

.

println

(

"Original Vector: "

fruits

)

;

       

       

// Use the set() method to replace an element

       

String

oldFruit

=

fruits.

set

(

1

,

"Blueberry"

)

;

       

       

System

.

out

.

println

(

"After replacement: "

oldFruit

)

;

       

System

.

out

.

println

(

"Vector after modification: "

fruits

)

;

   

}

}

Output result:

原始 Vector: [Apple, Banana, Cherry]
被替换的元素: Banana
修改后的 Vector: [Apple, Blueberry, Cherry]

Handle exceptional cases

Example

import

java.util.Vector

;

public

class

VectorSetException

{

   

public

static

void

main

(

String

[

]

args

)

{

        Vector

<

Integer

numbers

=

new

Vector

<>

(

)

;

        numbers.

add

(

10

)

;

        numbers.

add

(

20

)

;

       

       

try

{

           

// Try to set a nonexistent index

            numbers.

set

(

3

,

30

)

;

       

}

catch

(

IndexOutOfBoundsException

e

)

{

           

System

.

out

.

println

(

"Error: "

e.

getMessage

(

)

)

;

       

}

   

}

}

Output result:

错误: Index: 3, Size: 2

Method Characteristics

Thread Safety

Vector is a thread-safe collection class, so the set() method is also thread-safe and can be used in a multithreaded environment without additional synchronization.

Time Complexity

set() The method's time complexity is O(1) because it can access elements directly by index.

Comparison with ArrayList's set()

Feature Vector.set() ArrayList.set()
Thread Safety Is No
Performance Slightly slower (because of synchronization overhead) is usually faster than
Exception handling are the same are the same

Practical Application Scenarios

Batch Data Updates

Example

Vector

<

Double

temperatures

=

new

Vector

<>

(

)

;

// Assume temperatures has already been populated with data

// Batch-update temperature values (e.g., increase by 1 degree)

for

(

int

i

=

0

;

i

<

temperatures.

size

(

)

;

i

++

)

{

    temperatures.

set

(

i, temperatures.

get

(

i

)

1.0

)

;

}

State Updates in Game Development

Example

Vector

<

Character

gameCharacters

=

new

Vector

<>

(

)

;

// Initialize game characters

// Update the state of a character

gameCharacters.

set

(

characterIndex, updatedCharacter

)

;

Data Modification in a Multithreaded Environment

Example

// Safely modify shared data in a multithreaded environment

Vector

<

String

sharedData

=

new

Vector

<>

(

)

;

// Thread 1

new

Thread

(

(

)

->

{

    sharedData.

set

(

0

,

"Thread1 update"

)

;

}

)

.

start

(

)

;

// Thread 2

new

Thread

(

(

)

->

{

    sharedData.

set

(

1

,

"Thread2 update"

)

;

}

)

.

start

(

)

;


Notes

Index Range Check

When using set() method, it is best to check whether the index is valid first:

Example

if

(

index

=

0

&&

index

<

vector.

size

(

)

)

{

    vector.

set

(

index, newElement

)

;

}

else

{

   

// Handle the invalid index case

}

Null Value Handling

Vector allows storing null value, therefore set() method can also be used to set null:

Example

vector.

set

(

1

,

null

)

;

// This is legal

Performance considerations

When thread safety is not needed, consider using ArrayList instead of Vector, because ArrayList 's set() method has no synchronization overhead and performs better.


Summary

Vector.set() method is an effective way to modify Vector elements in the collection; it has the following characteristics:

  1. Can replace elements directly by index
  2. Return the replaced old element
  3. Is a thread-safe operation
  4. High operation efficiency (O(1) time complexity)

When using it, pay attention to the index range to avoid IndexOutOfBoundsException. Choose whether to use Vector(needs thread safety) or ArrayList(pursues higher performance) based on the application scenario.

Java Vector Java Vector