As we know the default size of ArrayList is 10,and when we add any element its size gets incremented by one and allocates the memory for that value and copy the existing one to newly allocated memory.so what what does it mean is the 10 allocated memory leave blank or after the 1oth element has been completed it gets resized
ArrayList uses an array to store the elements. Arrays have a fixed size. The array that ArrayList uses has to have a default size, obviously. 10 is probably a more or less arbitrary number for the default number of elements. When you create a new ArrayList with nothing in it, then ArrayList will have made an array of 10 elements behind the scenes. Ofcourse those 10 elements are all null.
Every time the array is full, ArrayList creates a new, larger array and copies the elements from the old array to the new array. You don't want that to happen every time that you add an element to the ArrayList (copying the array takes time, especially if the array is large), so ArrayList does it in steps of 10 or so.
If you want to know exactly how it works, look for the file src.zip in your JDK installation directory. Open it and lookup the source code for java.util.ArrayList in there.
The original poster appears to be talking about both capacity and size, mixing them up. This is confusing; please use terminology carefully. For a newly created arrayList, the default capacity is 10, but the size is 0. Each time you add an element, the size increases by 1, and the capacity usually stays the same. When it does need to increase, the capacity increases by about 50%,
Campbell Ritchie wrote:It is not quite increase by 50%. otherwise a 1‑capacity List would never enlarge.
Ha! Good point!
posted 7 years ago
Which is why I said about 50%. It's an approximation. And there are other corner cases as well, dealing with overflow for example. In the case of a 1-capacity list, "not quite increase by 50%" is wrong, of course - it necessarily increases by more than 50%. I suppose we could enumerate all the details of this, implementation-specific as they are, but those details are of course available in the source code to those who feel they need them.