capacity() and size() of Vector in Java

The difference between capacity() and size() in java.util.Vector is that the size() is the number of elements which is currently hold and capacity() is the number of element which can maximum hold. A Vector is a dynamically growable data structure, and it would reallocate its backing array as necessary. Thus, there is no final capacity, but you can set what its initial value is. A Vector defaults to doubling the size of its array. Example
import java.util.*; class TestClass { public static void main (String[] args) throws java.lang.Exception { //create new Vector Object Vector vcTr = new Vector(); System.out.println("Vector Size: " + vcTr.size()); vcTr.setSize(2); System.out.println("Vector Size: " + vcTr.size()); vcTr.addElement("Sunday"); vcTr.addElement("Monday"); vcTr.addElement("Wednesday"); System.out.println("Vector Size: " + vcTr.size()); System.out.println("Vector Capacity: " + vcTr.capacity()); } }
Vector Size: 0 Vector Size: 2 Vector Size: 5 Vector Capacity: 10

Vector in Java

Java Vectors are commonly used instead of arrays , because they expand automatically when new data is added to them. That means the Vector instances, like linked-lists , can grow dynamically . However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created. More about.... Vector in Java