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

Java Vector capacity() Method

Java Vector capacity() Method

Java Vector Java Vector

capacity() The method is in Java's Vector A 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.

Syntax format


public int capacity()

Method Return Value

The method returns a int type value, representing the current Vector 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 result

当前Vector容量: 10

In-Depth Understanding

Vector Capacity vs Size

Example

Vector

<
Integer
numbers
=
new
Vector
<>
(
5
)

;

// Initial capacity is 5

numbers.

add

(

1

)

;

numbers.

add

(

2

)

;

System

.

out

.

println

(

"Capacity: "

numbers.

capacity

(

)

)

;

// Output 5

System

.

out

.

println

(

"Size: "

numbers.

size

(

)

)

;

   

// Output 2

Automatic Capacity Growth Mechanism

When the size of the Vector exceeds the 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, auto-grows

System

.

out

.

println

(

"Capacity after growth: "

vec.

capacity

(

)

)

;

// 5 (3 + 2)


Best Practices

  1. Set a Reasonable Initial Capacity: If the number of elements can be estimated, setting an appropriate initial capacity can reduce the performance overhead of 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 not to add more elements, you can use trimToSize() to adjust the capacity to the current size and save memory

Example

Vector

<
String
names
=
new
Vector
<>
(
100
)

;

// Add elements...

names.

trimToSize

(

)

;

// Adjust the capacity to the current size


Common questions

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

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

This is caused by the Vector's automatic expansion mechanism. When the Vector needs to expand, it adds 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 the trimToSize() method to adjust the capacity of the Vector to the 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 number of elements actually stored.

Java Vector Java Vector