0
ArrayList<String> Array=new ArrayList<>(5);
Array.add("Ten");
Array.add("Twenty");
Array.add(4,"Fifty");
Array.add("Thirty");
Array.add("Fourty");
System.out.println(Array);

I got the IndexOutOfBoundsException exception when I run this code. Can anyone explain the reason why I am getting this error?

Ivar
  • 4,655
  • 12
  • 45
  • 50

6 Answers6

2

I think this is caused by your misunderstanding of the first argument to the ArrayList constructor. It is not the initial size you are providing, but the capacity.

The ArrayList class works with both a size and capacity. The size is the actual number of added elements within the list. The capacity, however, is the size of the internal array of ArrayList where the actual elements are stored. If enough elements are added, the capacity is increased by copying all elements of the current internal array to a new array with double the size of the current one. This is done because otherwise you have to expand the internal array each time an element is added. That would seriously impact performance.

The single-argument constructor accepts the initial capacity, and not the size. It's common not to specify the capacity, because the capacity is initialized to a sensible default. This constructor, however, exists mainly for performance reasons: if it is known that the number of elements will become very large, one could set the capacity through that constructor, so the ArrayList does not have to resize a lot of times.

MC Emperor
  • 17,266
  • 13
  • 70
  • 106
1

You are trying to add "Fifty" at index 4, which is not available because the size of ArrayList is 2 at that point of execution.

Array.add(4,"Fifty"); Adding "Fifty" at index 4

Ranjit Soni
  • 323
  • 1
  • 12
1

If you check out the docs, it clearly mentions that:

Throws: IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())

At the point of execution, your list size will be 2 and 4 > size i.e 2. Hence, this exception will be thrown.

You can check out the same discussion in Stack Overflow as well.

Suraj Gautam
  • 1,231
  • 9
  • 20
  • 1
    There are some more comments about this topic on https://stackoverflow.com/questions/8896758/initial-size-for-the-arraylist – Kaj Hejer Dec 17 '20 at 15:16
1

From the void java.util.ArrayList.add(int index, Object element) method declaration :

Throws: IndexOutOfBoundsException - if the index is out of range(index < 0 || index > size())

So you cant add to index number 4 because there is not one at the time the method is called

AirlineDog
  • 518
  • 4
  • 18
0

Your Problem is Array.add(4,"Fifty") at this moment you doesn't have index 4.

Deceptio
  • 33
  • 1
  • 5
0

Your code declares the capacity of your ArrayList, not its actual size. So you can add elements, but adding to position 4 fails because that position doesn't yet exist.

You can check that by looking at the size of your array as you add elements:

System.out.println(array.size());

Adding a value increases your array's size by 1. But if your array's size is only 2, you'll get an error if you try adding a value to position 4.

Depending on your end goal, you could add a couple blank values in order to initialize those positions, then add to position 4:

ArrayList<String> array = new ArrayList<>(5);

Then you can do your adds:

array.add("Ten");
array.add("Twenty");
array.add("");
array.add("");
array.add(4,"Fifty");
array.add("Thirty");
array.add("Fourty");
System.out.println(array);
J Woodchuck
  • 2,265
  • 24
  • 44