Java Vector capacity() Example

The capacity() method of java.util.Vector class will return the current capacity of the vector.

Signature

public int capacity()

This method returns the current capacity of the vector (ie) the length of its internal data array, kept in the field elementData of this vector

Example

The following example shows the usage of java.util.Vector.capacity() method.

import java.util.Vector;

public class CapacityMethodVectorExample 
{	
	public static void main(String args[])
	{
		// Create vector1 object
		Vector vector1 = new Vector();
						
		//adding elements to Vector using add()
		vector1.add("Element1");
		vector1.add("Element2");
		vector1.add("Element3");
		vector1.add("Element4");
		
		System.out.println("Capacity of the Vector is : "+vector1.capacity());
	}
}

Output

Capacity of the Vector is : 10

Leave a Reply

Your email address will not be published. Required fields are marked *