Understand Arraylist IndexOutOfBoundsException in Android
android, arraylist, indexoutofboundsexception, java
Solution
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:
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
Problem
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.