简体   繁体   English

了解Android中的Arraylist IndexOutOfBoundsException

[英]Understand Arraylist IndexOutOfBoundsException in Android

I get a lot of IndexOutOfBoundsException from the any Arraylist I use. 我从我使用的任何Arraylist获得了很多IndexOutOfBoundsException Most of the times it works fine but sometimes I get this annoying error on Arraylists i use on my project. 在大多数情况下,它运行良好,但有时我在我的项目中使用的Arraylists上出现此烦人的错误。

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. 帮助我了解此错误的主要原因,因为无论我搜索了多少答案,它们都无法完全帮助我。

java.util.ArrayList.throwIndexOutOfBoundsException: Invalid index 3, size is 3 java.util.ArrayList.throwIndexOutOfBoundsException:无效的索引3,大小为3

It means you have ArrayList which having 3 elements where you can get each element like 0,1,2 positions. 这意味着您拥有ArrayList,其中包含3个元素,您可以在其中获取每个元素,例如0,1,2位置。 And you are trying to read 4th element which does not exists into ArrayList. 并且您正在尝试读取ArrayList中不存在的第4个元素。

java.util.ArrayList.throwIndexOutOfBoundsException: Invalid index 0, size is 0 java.util.ArrayList.throwIndexOutOfBoundsException:无效的索引0,大小为0

It means you have a empty ArrayList, and you are trying to read 1st element. 这意味着您有一个空的ArrayList,并且您正在尝试读取第一个元素。


ArrayIndexOutOfBoundsException - Examples ArrayIndexOutOfBoundsException-示例

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". 数组索引越界异常是由于程序正在尝试访问超出数组限制的位置的元素而抛出的Java异常,因此出现了“ 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. 上图包含一个由7个元素组成的数组。 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. 在Java中,索引始终以0开头,以数组-1的长度结尾。 For example, the array above consists of 7 elements, therefore it's indices start from 0 and end with 6 (7-1). 例如,上面的数组由7个元素组成,因此其索引从0开始,以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. 尝试访问索引小于0或大于6的元素将导致Java引发ArrayIndexOutOfBoundsException。


Read more about ArrayIndexOutOfBoundsException - Examples, Causes & Fixes 阅读有关ArrayIndexOutOfBoundsException的更多信息-示例,原因和修复

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 . ArrayList大小为3时,您可以访问位置0 1和2处的项目。如果尝试访问位置3处的项目,它将抛出IndexOutOfBoundsException

So when you iterate through Arraylist, your for loop should be like this 因此,当您遍历Arraylist时,您的for循环应如下所示

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

Your condition must be i< list.size() 您的条件必须为i< list.size()

It is as simple as it gets. 就这么简单。

java.util.ArrayList.throwIndexOutOfBoundsException: Invalid index 3, size is 3 java.util.ArrayList.throwIndexOutOfBoundsException:无效的索引3,大小为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. 它说size为3,而您要的索引也是3。数组列表以0开头,因此如果size为3,则最大索引将为2。

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. 这意味着您的arraylist大小= 3,但是您要访问arraylist中的index = 3。 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的索引从0开始,因此,如果arraylist的大小= 3,则意味着您可以像这样从0到2访问索引

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

Size count starts from 1,2,3..... 大小从1,2,3 .....开始

Index count starts from 0,1,2.... 索引计数从0,1,2 ....开始

When your arry list size is 1. you get value using 0 index. 当您的到达列表大小为1时,您将使用0索引获得值。 if u send index value 1. its throw exception. 如果您发送索引值1.它的throw异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM