Java Vector setSize() Method

Java Vector Java Vector


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: specifiesVectorthe adjusted size

Parameter Characteristics

  • IfnewSizegreater than the current size,Vectorit will automatically fillnullnull values into the newly added positions
  • IfnewSizeless than the current size,Vectorit will truncate the excess elements
  • IfnewSizeequal to the current size,Vectorit remains unchanged

Method Behavior

Expansion Scenario

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 the vector contains ["A", "B", "C", null, null]

Shrinkage Scenario

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 the vector contains ["A", "B"]

Equal Scenario

When the new size equals the current size:

Example

Vector<String> vector = new Vector<>(Arrays.asList("A", "B", "C"));
vector.setSize(3);
// the vector remains unchanged, still ["A", "B", "C"]

Usage Example

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("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:

  1. Preallocating space: When you know you need to store a certain number of elements, you can set the size in advance
  2. Clearing extra space: When some elements are no longer needed, you can shrinkVectorto save memory
  3. Resetting the container: Quickly clearVectorand set a new size

Example

// Example of preallocating space
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 callssetSize()may cause performance degradation, especially with large amounts of data
  • If you need to frequently resize, consider usingensureCapacity()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

// Using ArrayList to implement similar functionality
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 Java Vector

Other Extensions