The below code ensures a capacity of 11 internally,
ArrayList list = new ArrayList(11);
so why/when should I use public method ensureCapacity() externally?
list.ensureCapacity(11);
And if there is no use why it is kept public?
public void ensureCapacity(int minCapacity) {
int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
// any size if not default element table
? 0
// larger than default for default empty table. It's already
// supposed to be at default size.
: DEFAULT_CAPACITY;
if (minCapacity > minExpand) {
ensureExplicitCapacity(minCapacity);
}
}