Java Vector ensureCapacity() Method

The ensureCapacity() method of Java Vector class is used to increase the capacity of the vector which is in use, if necessary. It ensures that the vector can hold at least the number of components specified by the minimum capacity argument.

Syntax:

Following is the declaration of ensureCapacity() method:

Parameter:

DataTypeParameterDescriptionRequired/Optional
intminCapacityIt is the desired minimum capacity.Required

Returns:

The ensureCapacity() method has return type void so, it does not return anything.

Exceptions:

NA

Compatibility Version:

Java 1.2 and above

Example 1:

Test it Now

Output:

Minimum capacity of this vector: 25

Example 2:

Test it Now

Output:

Element: [50, green, red]
Capacity of the vector is: 10
New Capacity of the vector is:20

Example 3:

Test it Now

Output:

Element of the vector: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Vector size: 10
Vector element after ensuring capacity: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
New vector size: 20

Next TopicJava Vector




Latest Courses