94
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:
DataType | Parameter | Description | Required/Optional |
---|---|---|---|
int | minCapacity | It 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:
Output:
Minimum capacity of this vector: 25
Example 2:
Output:
Element: [50, green, red] Capacity of the vector is: 10 New Capacity of the vector is:20
Example 3:
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