Java Vector setSize() Method
setSize() is a method in Java's Vector An important method provided by the class, used to set a new size for the vector. This method can dynamically adjust the Vector 's capacity so that it meets the needs of the running program.
Method Syntax:
public void setSize(int newSize)
Method parameters
Parameter Description
newSize: This is the method's only parameter, indicating theVector's new size you want to set- Type:
int(integer) - Effect: Specifies the
Vectoradjusted size
Parameter Characteristics
- If both the
newSizeis greater than the current size, theVectorautomatically fillsnullvalues into the newly added positions - If both the
newSizeis less than the current size, theVectortruncates the extra elements - If both the
newSizeequals the current size, itVectorremains unchanged
Method behavior
Expansion Case
When the new size is greater than the current size:
Example
Vector
<
String
vector
=
new
Vector
<>
(
Arrays
.
asList
(
"A"
,
"B"
,
"C"
)
)
;
vector.
setSize
(
5
)
;
// Now vector contains ["A", "B", "C", null, null]
Shrinking Case
When the new size is less than the current size:
Example
Vector
<
String
vector
=
new
Vector
<>
(
Arrays
.
asList
(
"A"
,
"B"
,
"C"
,
"D"
)
)
;
vector.
setSize
(
2
)
;
// Now vector contains ["A", "B"]
Equal Case
When the new size equals the current size:
Example
Vector
<
String
vector
=
new
Vector
<>
(
Arrays
.
asList
(
"A"
,
"B"
,
"C"
)
)
;
vector.
setSize
(
3
)
;
// vector remains unchanged, still ["A", "B", "C"]
Usage examples
Basic example
Example
import
java.util.Vector
;
public
class
VectorSetSizeExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create an initial Vector
Vector
<
Integer
numbers
=
new
Vector
<>
(
)
;
numbers.
add
(
10
)
;
numbers.
add
(
20
)
;
numbers.
add
(
30
)
;
System
.
out
.
println
(
"Original Vector: "
numbers
)
;
// Expand the Vector
numbers.
setSize
(
5
)
;
System
.
out
.
println
(
"Vector after expansion: "
numbers
)
;
// Shrink the Vector
numbers.
setSize
(
2
)
;
System
.
out
.
println
(
"Vector after shrinking: "
numbers
)
;
}
}
Output result:
原始 Vector: [10, 20, 30]
扩大后的 Vector: [10, 20, 30, null, null]
缩小后的 Vector: [10, 20]
Practical Application Scenarios
setSize() The method is especially useful in the following scenarios:
- Pre-allocating Space: When you know you need to store a certain number of elements, you can set the size in advance
- Cleaning Up Excess Space: When some elements are no longer needed, you can shrink the
Vectorto save memory - Resetting the Container: Quickly clear the
Vectorand set a new size
Example
// Pre-allocation example
Vector
<
String
names
=
new
Vector
<>
(
)
;
names.
setSize
(
100
)
;
// Pre-allocate 100 positions
// Cleaning up excess space example
Vector
<
Double
prices
=
new
Vector
<>
(
)
;
// ... after adding many elements ...
prices.
setSize
(
10
)
;
// Keep only the first 10 elements
// Container reset example
Vector
<
Object
tempStorage
=
new
Vector
<>
(
)
;
// ... after use ...
tempStorage.
setSize
(
0
)
;
// Clear all elements
Notes
Performance considerations
- Frequent calls
setSize()may cause performance degradation, especially with large amounts of data - If the size needs to be adjusted frequently, consider using the
ensureCapacity()method to pre-allocate enough space
Thread Safety
Vectoris thread-safe,setSize()The method is also synchronized- It is safe to use in a multithreaded environment but may affect performance
Alternatives
In the Java collections framework,ArrayList is a more modern alternative, but it does not have a direct setSize() method. If similar functionality is needed, consider:
Example
// Implement similar functionality using ArrayList
ArrayList
<
String
list
=
new
ArrayList
<>
(
)
;
// Expand
while
(
list.
size
(
)
<
newSize
)
{
list.
add
(
null
)
;
}
// Shrink
while
(
list.
size
(
)
newSize
)
{
list.
remove
(
list.
size
(
)
1
)
;
}
Summary
Vector 's setSize() method provides a direct way to adjust the vector's size, whether expanding or shrinking. Although Vector in modern Java programming is not as ArrayList commonly used as, it still has value in certain specific scenarios (such as needing thread safety or maintaining legacy code). Understanding this method's behavior and characteristics can help you use it more effectively Vector collections.
Java Vector