Javatpoint Logo
Javatpoint Logo

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.


Following is the declaration of ensureCapacity() method:


DataType Parameter Description Required/Optional
int minCapacity It is the desired minimum capacity. Required


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



Compatibility Version:

Java 1.2 and above

Example 1:

Test it Now


Minimum capacity of this vector: 25

Example 2:

Test it Now


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

Example 3:

Test it Now


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

Youtube For Videos Join Our Youtube Channel: Join Now


  • Send your Feedback to

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA