Click here to watch in Youtube :https://www.youtube.com/watch?v=3MaZyBXc-_w&list=UUhwKlOVR041tngjerWxVccwVectorExample.java import java.util.Vector;
/*
* Example of capacity() method
*/
public class VectorExample
{
public static void main(String[] args)
{
Vector<Integer> vector = new Vector<Integer>();
System.out.println("vector : " + vector);
System.out.println("vector size : " + vector.size());
/*
* Returns the current capacity of this vector.
*/
int capacity = vector.capacity();
System.out.println("capacity : " + capacity + "\n");
for (int i = 0; i < 11; i++)
{
vector.add(i+1);
}
System.out.println("vector : " + vector);
System.out.println("vector size : " + vector.size());
capacity = vector.capacity();
System.out.println("capacity : " + capacity + "\n");
int j = vector.size()+1;
for (int i = 0; i < 11; i++)
{
vector.add(i+j);
}
System.out.println("vector : " + vector);
System.out.println("vector size : " + vector.size());
capacity = vector.capacity();
System.out.println("capacity : " + capacity + "\n");
}
}
Output vector : []
vector size : 0
capacity : 10
vector : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
vector size : 11
capacity : 20
vector : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
vector size : 22
capacity : 40