Initial size for the ArrayList

You can set the initial size for an ArrayList by doing

ArrayList<Integer> arr=new ArrayList<Integer>(10);

However, you can’t do

arr.add(5, 10);

because it causes an out of bounds exception.

What is the use of setting an initial size if you can’t access the space you allocated?

The add function is defined as add(int index, Object element) so I am not adding to index 10.

17 Answers
17

Leave a Comment