In the previous article, we have discussed about Java Vector trimToSize( ) Method with Example
In this article, we are going to see the use of Java Vector capacity() method along with examples.
Java Vector capacity( ) Method with Example
This java.util.Vector.capacity() method gives the minimum capacity of the vector. It returns the capacity of the vector.
Syntax:
vectorName.capacity( )
Where,
vectorNamerefers to the name of the Vector.
Approach:
- Create a vector of string data type and add some elements to it.
- Print the elements.
- Print the vector capacity using
capacity( )method.
Program:
import java.util.*;
public class Main
{
public static void main(String[] args) {
// Create a Vector of string datatype
Vector<String> vec = new Vector<String>();
// Adding some elements to the vector
vec.add("One");
vec.add("Two");
vec.add("Three");
vec.add("Four");
vec.add("Five");
vec.add("Six");
vec.add("Seven");
// Prints the vector elements
System.out.println("The vector elements are "+vec);
// Prints the capacity of the vector
System.out.println("The capacity of the vector is "+vec.capacity());
}
}
Output: The vector elements are [One, Two, Three, Four, Five, Six, Seven] The capacity of the vector is 10
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
Related Java Programs: