Understand Arraylist IndexOutOfBoundsException in Android

I get a lot of IndexOutOfBoundsException from the any Arraylist I use. Most of the times it works fine but sometimes I get this annoying error on Arraylists i use on my project.

The main cause is always either

java.util.ArrayList.throwIndexOutOfBoundsException: Invalid index 3, size is 3

or

java.util.ArrayList.throwIndexOutOfBoundsException: Invalid index 0, size is 0 

Help me understand the main cause of this error as no matter how many answers I've searched they don't completely help me.


Solution 1:

java.util.ArrayList.throwIndexOutOfBoundsException: Invalid index 3, size is 3

It means you have ArrayList which having 3 elements where you can get each element like 0,1,2 positions. And you are trying to read 4th element which does not exists into ArrayList.

java.util.ArrayList.throwIndexOutOfBoundsException: Invalid index 0, size is 0

It means you have a empty ArrayList, and you are trying to read 1st element.


ArrayIndexOutOfBoundsException - Examples

An array-index out of bounds exception is a Java exception thrown due to the fact that the program is trying to access an element at a position that is outside an array limit, hence the words "Out of bounds". In other words, the program is trying to access an element at an index that is outside the array bounds. To understand array bounds, let us consider the following diagram:

enter image description here

The picture above contains an array that consists of 7 elements. Each element in the array has its own index/position. In Java, an index always starts with 0 and ends with the length of the array -1. For example, the array above consists of 7 elements, therefore it's indices start from 0 and end with 6 (7-1). Attempting to access an element with an index less than 0 or more than 6 will cause Java to throw an ArrayIndexOutOfBoundsException.


Read more about ArrayIndexOutOfBoundsException - Examples, Causes & Fixes

Solution 2:

When your ArrayList size is 3, you can access the items at position 0 1 and 2. If you try to access the item at position 3, it will throw an IndexOutOfBoundsException.

So when you iterate through Arraylist, your for loop should be like this

for(int i=0; i< list.size(); i++){
   Object data = list.get(i);
}

Your condition must be i< list.size()

Solution 3:

It is as simple as it gets.

java.util.ArrayList.throwIndexOutOfBoundsException: Invalid index 3, size is 3

It says size is 3 and the index you are asking for is also 3. Array list start with 0 so the maximum index will be 2 if the size is 3.

Solution 4:

java.util.ArrayList.throwIndexOutOfBoundsException: Invalid index 3, size is 3

this means your arraylist size = 3, but you want to access index = 3 in arraylist. You need to know the index start at 0 in arraylist so that if your arraylist size = 3 that means you can access index from 0 to 2 like this

arraylist.get(0)
arraylist.get(1)
arraylist.get(2)

Solution 5:

Size count starts from 1,2,3.....

Index count starts from 0,1,2....

When your arry list size is 1. you get value using 0 index. if u send index value 1. its throw exception.