- Capacity is the number of elements a vector can hold before resizing its internal data structures.
- For performance reasons, it's best to reduce the number of times the internal structure has to resize.
- If the capacity is too large, memory goes to waste.
- To find out the current capacity of a vector: the capacity() method:
import java.util.Vector;
public class MainClass {
public static void main(String args[]) {
Vector v = new Vector(5);
for (int i = 0; i < 10; i++) {
v.add(0,i);
}
System.out.println(v);
System.out.println(v.size());
for (int i = 0; i < 10; i++) {
v.add(0,i);
}
System.out.println(v);
System.out.println(v.size());
}
}
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
10
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
20