Understand Arraylist IndexOutOfBoundsException in Android

I get a lot IndexOutOfBoundsExceptionfrom any used Arraylist. In most cases, it works fine, but sometimes I get this annoying bug in Arraylistswhich I use in my project.

The main reason is always

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 root cause of this error, no matter how many answers I searched, they do not completely help me.

0
source share
5 answers

java.util.ArrayList.throwIndexOutOfBoundsException: invalid index 3, size 3

, ArrayList, 3 , , , 0,1,2 . 4- , ArrayList.

java.util.ArrayList.throwIndexOutOfBoundsException: 0, 0

, ArrayList, 1- .


ArrayIndexOutOfBoundsException -

- Java, , , , , " ". , , . , :

enter image description here

, 7 . /. Java 0 -1. , 7 , 0 6 (7-1). 0 6 , Java ArrayIndexOutOfBoundsException.


ArrayIndexOutOfBoundsException - ,

+9

ArrayList 3, 0 1 2. 3, IndexOutOfBoundsException.

, Arraylist, for

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

i< list.size()

+2

, .

java.util.ArrayList.throwIndexOutOfBoundsException: 3, 3

, 3, , , 3. 0, 2, 3.

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

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

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

Size starts at 1,2,3 .....

The number of indices starts at 0,1,2 ....

When your arry list size is 1., you get the value using index 0. if u sends the value of index 1. its throw exception.

+1
source

Source: https://habr.com/ru/post/1688556/


All Articles