Java 了解 Android 中的 Arraylist IndexOutOfBoundsException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21570318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understand Arraylist IndexOutOfBoundsException in Android
提问by lil'ms
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.
我IndexOutOfBoundsException
从Arraylist
我使用的任何东西中得到了很多。大多数时候它工作正常,但有时我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.
帮助我了解此错误的主要原因,因为无论我搜索了多少答案,它们都不能完全帮助我。
采纳答案by Pankaj Kumar
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. And you are trying to read 4th element which does not exists into ArrayList.
这意味着您有 ArrayList,其中有 3 个元素,您可以在其中获取每个元素,例如 0,1,2 位置。并且您正在尝试将不存在的第四个元素读取到 ArrayList 中。
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". 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:
数组索引越界异常是由于程序试图访问位于数组限制之外的位置的元素而引发的 Java 异常,因此称为“越界”。换句话说,程序试图访问位于数组边界之外的索引处的元素。为了理解数组边界,让我们考虑下图:
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.
上图包含一个由 7 个元素组成的数组。数组中的每个元素都有自己的索引/位置。在 Java 中,索引总是从 0 开始,以数组的长度 -1 结束。例如,上面的数组由 7 个元素组成,因此它的索引从 0 开始,以 6 结束(7-1)。尝试访问索引小于 0 或大于 6 的元素将导致 Java 抛出 ArrayIndexOutOfBoundsException。
Read more about ArrayIndexOutOfBoundsException - Examples, Causes & Fixes
回答by Mayank Saini
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.
它说大小为 3,您要求的索引也是 3。数组列表从 0 开始,因此如果大小为 3,则最大索引将为 2。
回答by henry4343
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
这意味着您的数组列表大小 = 3,但您想访问数组列表中的索引 = 3。您需要知道数组列表中的索引从 0 开始,以便如果您的数组列表大小 = 3,则意味着您可以像这样访问从 0 到 2 的索引
arraylist.get(0)
arraylist.get(1)
arraylist.get(2)
回答by ravindra.kamble
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()
回答by RVG
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. if u send index value 1. its throw exception.
当您的 arry 列表大小为 1 时,您使用 0 索引获得价值。如果你发送索引值 1。它的抛出异常。