Java Vector capacity() Method

Java Vector Java Vector

capacity()The method is in JavaVectorA built-in method provided by the class, used to return the current capacity of the vector. Capacity refers to the number of elements the vector can store without reallocating its internal storage space.

Syntax Format

public int capacity()

Method Return Value

This method returns aintvalue of type, representing the currentVectorthe object's capacity.


Basic Usage

Example Code

Example

import java.util.Vector;

public class VectorCapacityExample {
    public static void main(String[] args) {
        // Create a Vector with an initial capacity of 10
        Vector<String> fruits = new Vector<>(10);
       
        // Add some elements
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");
       
        // Get the current capacity
        int currentCapacity = fruits.capacity();
        System.out.println("Current Vector capacity: " + currentCapacity);
    }
}

Output

当前Vector容量: 10

In-depth Understanding

Vector Capacity vs Size

  • Capacity: The size of the Vector's internal array, i.e., the total number of elements it can store
  • Size: The number of elements currently actually stored in the Vector

Example

Vector<Integer> numbers = new Vector<>(5); // Initial capacity is 5
numbers.add(1);
numbers.add(2);

System.out.println("Capacity: " + numbers.capacity()); // Outputs 5
System.out.println("Size: " + numbers.size());     // Outputs 2

Automatic Capacity Growth Mechanism

When the size of a Vector exceeds its current capacity, the Vector automatically increases its capacity. By default, the capacity doubles, but this behavior can be changed by specifying a capacity increment.

Example

// Create a Vector with an initial capacity of 3 and an increment of 2
Vector<Integer> vec = new Vector<>(3, 2);

System.out.println("Initial capacity: " + vec.capacity()); // 3

vec.add(1);
vec.add(2);
vec.add(3);
vec.add(4); // Exceeds the initial capacity, automatically grows

System.out.println("Capacity after growth: " + vec.capacity()); // 5 (3 + 2)

Best Practices

  1. Set a reasonable initial capacity: If you can estimate the number of elements, setting an appropriate initial capacity can reduce the performance overhead caused by automatic expansion.
  2. Avoid frequent expansion: Frequent expansion affects performance, especially when processing large amounts of data.
  3. **Use trimToSize() when appropriate**: If you are sure that no more elements will be added, you can usetrimToSize()to adjust the capacity to the current size and save memory.

Example

Vector<String> names = new Vector<>(100);
// Add elements...
names.trimToSize(); // Adjust capacity to the current size

Common Questions

Q1: What is the difference between capacity() and size()?

  • capacity()Returns the total number of elements the Vector can currently store
  • size()Returns the number of elements currently actually stored in the Vector

Q2: Why is my Vector's capacity much larger than the actual number of elements?

This is caused by Vector's automatic expansion mechanism. When a Vector needs to expand, it increases enough space to avoid frequent expansion, which may cause the capacity to be much larger than the current number of stored elements.

Q3: How can I reduce the capacity of a Vector?

You can use thetrimToSize()method to adjust the Vector's capacity to its current size:

vector.trimToSize();

Summary

capacity()The method is one of the important methods for understanding and using Java Vector. By managing the Vector's capacity reasonably, you can improve program performance and optimize memory usage. Remember, capacity is the size of the Vector's internal storage space, while size is the actual number of stored elements.

Java Vector Java Vector

Other Extensions