Java Vector setSize() Method
setSize()is in JavaVectoran important method provided by the class, used to set the new size of the vector. This method can dynamically adjustVectorthe capacity, enabling it to meet the runtime requirements of the program.
Method Syntax:
public void setSize(int newSize)
Method Parameter
Parameter Description
newSize: This is the only parameter of the method, indicating theVectornew size- Type:
int(integer) - Purpose: specifies
Vectorthe adjusted size
Parameter Characteristics
- If
newSizegreater than the current size,Vectorit will automatically fillnullnull values into the newly added positions - If
newSizeless than the current size,Vectorit will truncate the excess elements - If
newSizeequal to the current size,Vectorit remains unchanged
Method Behavior
Expansion Scenario
When the new size is greater than the current size:
Example
vector.setSize(5);
// now the vector contains ["A", "B", "C", null, null]
Shrinkage Scenario
When the new size is less than the current size:
Example
vector.setSize(2);
// now the vector contains ["A", "B"]
Equal Scenario
When the new size equals the current size:
Example
vector.setSize(3);
// the vector remains unchanged, still ["A", "B", "C"]
Usage Example
Basic Example
Example
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("Expanded Vector: " + numbers);
// Shrink the Vector
numbers.setSize(2);
System.out.println("Shrunk Vector: " + numbers);
}
}
Output Result:
原始 Vector: [10, 20, 30] 扩大后的 Vector: [10, 20, 30, null, null] 缩小后的 Vector: [10, 20]
Practical Application Scenarios
setSize()The method is particularly useful in the following scenarios:
- Preallocating space: When you know you need to store a certain number of elements, you can set the size in advance
- Clearing extra space: When some elements are no longer needed, you can shrink
Vectorto save memory - Resetting the container: Quickly clear
Vectorand set a new size
Example
Vector<String> names = new Vector<>();
names.setSize(100); // Preallocate 100 positions
// Example of clearing extra space
Vector<Double> prices = new Vector<>();
// ... after adding many elements ...
prices.setSize(10); // Keep only the first 10 elements
// Example of resetting the container
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 you need to frequently resize, consider using
ensureCapacity()the method to preallocate sufficient space
Thread Safety
Vectoris thread-safe,setSize()the method is also synchronized- It is safe to use in a multi-threaded environment, but may affect performance
Alternatives
In the Java Collections Framework,ArrayListis a more modern alternative, but it does not have a directsetSize()method. If you need similar functionality, you can consider:
Example
ArrayList<String> list = new ArrayList<>();
// Expand
while (list.size() < newSize) {
list.add(null);
}
// Shrink
while (list.size() > newSize) {
list.remove(list.size() - 1);
}
Summary
VectorofsetSize()The method provides a direct way to adjust the size of the vector, whether expanding or shrinking. AlthoughVectorin modern Java programming is not asArrayListcommon, but it still has value in certain specific scenarios (such as needing thread safety or legacy code maintenance). Understanding the behavior and characteristics of this method can help you useVectorcollections.
Java Vector