The Wayback Machine - https://web.archive.org/web/20240618020227/https://www.geeksforgeeks.org/vector-capacity-method-in-java/
Open In App

Vector capacity() Method in Java

Last Updated : 17 Aug, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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:

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.

Below programs illustrate the Java.util.Vector.capacity() method:

Program 1: Vector with string elements.




// Java code to illustrate capacity()
import java.util.*;
  
public class VectorDemo {
    public static void main(String args[])
    {
        // Creating an empty Vector
        Vector<String> vec_tor = new Vector<String>();
  
        // Use add() method to add elements into the Vector
        vec_tor.add("Welcome");
        vec_tor.add("To");
        vec_tor.add("Geeks");
        vec_tor.add("4");
        vec_tor.add("Geeks");
  
        // Displaying the Vector
        System.out.println("Vector: " + vec_tor);
  
        // Displaying the capacity of Vector
        System.out.println("The capacity is: " + vec_tor.capacity());
    }
}


Output:

Vector: [Welcome, To, Geeks, 4, Geeks]
The capacity is: 10

Program 2: Vector with Integer elements.




// Java code to illustrate capacity()
import java.util.*;
  
public class VectorDemo {
    public static void main(String args[])
    {
        // Creating an empty Vector
        Vector<Integer> vec_tor = new Vector<Integer>();
  
        // Use add() method to add elements into the Vector
        vec_tor.add(10);
        vec_tor.add(15);
        vec_tor.add(30);
        vec_tor.add(20);
        vec_tor.add(5);
  
        // Displaying the Vector
        System.out.println("Vector: " + vec_tor);
  
        // Displaying the capacity of Vector
        System.out.println("The capacity is: " + vec_tor.capacity());
    }
}


Output:

Vector: [10, 15, 30, 20, 5]
The capacity is: 10


Previous Article
Next Article

Similar Reads

How to Initialize a Vector with a Specific Initial Capacity in Java?
In Java, Vector Class allows to creation of dynamic arrays that can grow or shrink as per the need. If we know the approximate size of elements, we will store this in the vector. Then we optimize memory usage, and we can initialize it with the specific initial capacity. In this article, we will learn how to initialize a Vector with a specific initi
2 min read
Stack capacity() method in Java with Example
The capacity() method of Stack Class is used to get the capacity of this Stack. That is the number of elements present in this stack container. Syntax: public int capacity() Parameters: This function accepts a parameter E obj which is the object to be added at the end of the Stack. Return Value: The method returns integer value which is the capacit
2 min read
Buffer capacity() method in Java with Examples
The capacity() method of java.nio.Buffer Class is used to return this buffer's capacity. Syntax: public final int capacity() Return Value: The capacity of this buffer Below are the examples to illustrate the capacity() method: Examples 1: // Java program to demonstrate // capacity() method import java.nio.*; import java.util.*; public class GFG { p
2 min read
Copy Elements of One Java Vector to Another Vector in Java
Vector is similar to arrays but is growable also, or we can say no fixed size is required. Previously vector was a part of legacy classes but now it is part of Collections. It also implements a List interface, so we can use any method of list interface on vectors also. Syntax : Vector&lt;Integer&gt; gfg=new Vector&lt;&gt;(); Ways To copy elements o
3 min read
Difference Between Length and Capacity in Java
Length and capacity are two different things in Java. Length basically increases when characters are appended to the string whereas the capacity increases when the current capacity is exceeded by a new length. length () method is a part of the Java String class. It is used to find the length of the string. It basically counts the number of characte
3 min read
StringBuilder capacity() in Java with Examples
The capacity() method of StringBuilder Class is used to return the current capacity of StringBUilder object. The capacity is the amount of storage available to insert new characters.Syntax: public int capacity() Return Value: This method returns the current capacity of StringBuilder Class.Below programs demonstrate the capacity() method of StringBu
2 min read
How to Create a HashSet With a Predefined Capacity in Java?
In Java, we can create HashSet with a predefined capacity by creating a constructor and passing the capacity as a parameter to it. We have to initialize the capacity using a variable otherwise you can pass a direct value to it. Syntax:HashSet &lt;datatype&gt;myset=new HashSet&lt;&gt;(capacity);Here, myset is the name of the set and capacity defines
1 min read
How does HashMap handle resizing when it reaches its capacity in Java?
The capacity of a HashMap simply doubles when it reaches a certain threshold. The threshold depends on the initial capacity and the load factor of the HashMap. Consider Capacity as the number of spaces, a HashMap currently has to store elements and Load factor is a measure of how full the map is allowed to get before its capacity is increased. Size
3 min read
How to Increase the capacity (size) of ArrayList?
ArrayList class is a resizable array, present in java.util package. The difference between an array and an ArrayList in Java, is that the size of an array cannot be modified (i.e. if you want to append/add or remove element(s) to/from an array, you have to create a new array. However, elements can be added/appended or removed from an ArrayList with
2 min read
Vector clear() Method in Java
The Java.util.Vector.clear() method is used to remove all the elements from a Vector. Using the clear() method only clears all the element from the vector and does not delete the vector. In other words, we can say that the clear() method is used to only empty an existing vector. Syntax: Vector.clear() Parameters: The method does not take any parame
2 min read