Jake Obrien wrote:
If the default size of an arraylist is 10, why does the following code print an IndexOutOfBoundsException?
I would have thought it should print Null? or at least throw a NullPointerException?
Jake Obrien wrote:
al.size returns 2. Therefore the 3rd element in the arraylist. The 3rd element should be Null, no?
Thanks,
Archana Rao
No. The default capacity is 10. A "capacity" is an implementation detail -- it is the size of the internal array used to hold the elements, and when the size exceeds the capacity, the collection will automatically allocate a larger internal larger array and copy the elements over. The users of the collection should not care about this detail.
Jake Obrien wrote:If the default size of an arraylist is 10, ...
Jake Obrien wrote:This is where my question stems from. Since the default capacity/size is 10... Why does Size() not return the actual size of the arraylist at Runtime? - Is this not 10?
| Consider Paul's rocket mass heater. |