Last Updated : 12 Dec, 2024
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() methodVector.capacity()
Below programs illustrate the Java.util.Vector.capacity() method:
Example 1: Vector with string elements.
Java
// 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());
}
}
Elements of Vector : [Welcome, To, GFG] The capacity is : 10
Example 2: Vector with Integer elements.
Java
// 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());
}
}
Elements of Vector : [10, 15, 30, 20] The capacity is : 10
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4