The Java.util.Vector.capacity() method in Java is used to get the capacity of the Vector or the length of the array present in the Vector.
Syntax of Vector capacity() method
Vector.capacity()
- Parameters: The method does not take any parameter.
- Return Value: The method returns the capacity or the internal data array’s length present in the Vector, which is an integer value.
Example of Vector capacity() Method
Below programs illustrate the Java.util.Vector.capacity() method:
Example 1: Vector with string elements.
// Java code to demonstrate
// capacity() Method
import java.util.*;
public class GFG
{
public static void main(String args[])
{
// Creating an empty Vector
Vector<String> v = new Vector<String>();
// Add elements in Vector
v.add("Welcome");
v.add("To");
v.add("GFG");
// Displaying the Vector
System.out.println("Elements of Vector : " + v);
// Displaying the capacity of Vector
System.out.println("The capacity is : " + v.capacity());
}
}
Output
Elements of Vector : [Welcome, To, GFG] The capacity is : 10
Example 2: Vector with Integer elements.
// Java code to demonstrate
// capacity() Method
import java.util.*;
public class GFG
{
public static void main(String args[])
{
// Creating an empty Vector
Vector<Integer> v = new Vector<Integer>();
// Add Elements in Vector
v.add(10);
v.add(15);
v.add(30);
v.add(20);
// Displaying the Vector
System.out.println("Elements of Vector : " + v);
// Displaying the capacity of Vector
System.out.println("The capacity is : " + v.capacity());
}
}
Output
Elements of Vector : [10, 15, 30, 20] The capacity is : 10