java.util.Vector.capacity() Method
Advertisements
Description
The capacity() method is used to return the current capacity of a vector.Capacity is the length of the array kept in the vactor.
Declaration
Following is the declaration for java.util.Vector.capacity() method
public int capacity()
Parameters
This method does not take any parameter as input.
Return Value
The method returns the current capacity of the vector as an integer value.Here capacity means the length of its internal data array, kept in the field elementData of this vector.
Exception
NA.
Example
The following example shows the usage of java.util.Vector.capacity() method.
package com.tutorialspoint;
import java.util.Vector;
public class VectorDemo {
public static void main(String[] args) {
// create an empty Vector vec
Vector<Integer> vec = new Vector<Integer>();
// use add() method to add elements in the vector
vec.add(14);
vec.add(13);
vec.add(12);
vec.add(11);
vec.add(4);
vec.add(3);
vec.add(2);
vec.add(1);
// let us print the capacity of the vector
System.out.println("Capacity of the vector is :"+vec.capacity());
}
}
Let us compile and run the above program, this will produce the following result:
Capacity of the vector is :10