ArrayIndexOutOfBoundsException是什么意思,我如何摆脱它?
下面是一个触发异常的代码示例:
String[] names = { "tom", "bob", "harry" };
for (int i = 0; i <= names.length; i++) {
System.out.println(names[i]);
}
ArrayIndexOutOfBoundsException是什么意思,我如何摆脱它?
下面是一个触发异常的代码示例:
String[] names = { "tom", "bob", "harry" };
for (int i = 0; i <= names.length; i++) {
System.out.println(names[i]);
}
当前回答
这就是在Eclipse中抛出这种类型的异常时的样子。红色的数字表示您试图访问的索引。所以代码看起来是这样的:
myArray[5]
当您试图访问该数组中不存在的索引时,将引发此错误。如果一个数组的长度是3,
int[] intArray = new int[3];
那么唯一有效的索引是:
intArray[0]
intArray[1]
intArray[2]
如果一个数组的长度为1,
int[] intArray = new int[1];
那么唯一有效的索引是:
intArray[0]
任何等于数组长度或大于数组长度的整数都是越界的。
任何小于0的整数:都是越界的;
附注:如果您希望更好地理解数组并做一些实际练习,这里有一个视频:Java中的数组教程
其他回答
对于给定的数组,数组的长度是3。的名字。但是因为它存储的是从索引0开始的元素,所以它的最大索引是2。
因此,代替'i**<=name。长度'你应该写'i<**name。长度',以避免'ArrayIndexOutOfBoundsException'。
这个简单的问题就讲到这里,我只想强调Java中的一个新特性,它可以避免所有关于数组中索引的困惑,即使是初学者。Java-8为您抽象了迭代的任务。
int[] array = new int[5];
//If you need just the items
Arrays.stream(array).forEach(item -> { println(item); });
//If you need the index as well
IntStream.range(0, array.length).forEach(index -> { println(array[index]); })
有什么好处?首先是可读性,比如英语。其次,您不需要担心ArrayIndexOutOfBoundsException
ArrayIndexOutOfBoundsException表示您正在尝试访问一个不存在或超出该数组界限的数组索引。数组索引从0开始,以长度- 1结束。
在你的情况下
for(int i = 0; i<=name.length; i++) {
System.out.print(name[i] +'\n'); // i goes from 0 to length, Not correct
}
当你试图访问时,ArrayIndexOutOfBoundsException会发生 这个名字。长度索引的元素不存在(数组索引以长度-1结束)。只需将<=替换为<就可以解决这个问题。
for(int i = 0; i < name.length; i++) {
System.out.print(name[i] +'\n'); // i goes from 0 to length - 1, Correct
}
此错误发生在运行循环超过限制次数时。让我们考虑一个简单的例子,
class demo{
public static void main(String a[]){
int[] numberArray={4,8,2,3,89,5};
int i;
for(i=0;i<numberArray.length;i++){
System.out.print(numberArray[i+1]+" ");
}
}
首先,我将数组初始化为“numberArray”。然后,使用for循环打印一些数组元素。当循环运行'i'时,打印(numberArray[i+1]元素..(当i的值为1时,打印numberArray[i+1]元素)..假设当i=(numberArray.length-2)时,打印数组的最后一个元素,当i的值转到(numberArray.length-1)时,没有值可打印,此时会出现ArrayIndexOutOfBoundsException。我希望你能明白。谢谢你!
什么原因导致ArrayIndexOutOfBoundsException?
如果您将变量视为一个可以放置值的“盒子”,那么数组就是一系列相邻放置的盒子,其中盒子的数量是一个有限且显式的整数。
创建一个这样的数组:
final int[] myArray = new int[5]
创建一行5个方框,每个方框包含一个int。每个方框都有一个索引,即一系列方框中的位置。这个索引从0开始,结束于N-1,其中N是数组的大小(盒子的数量)。
要从这一系列框中检索一个值,你可以通过它的索引来引用它,就像这样:
myArray[3]
这将给出该系列中第4个框的值(因为第一个框的索引为0)。
ArrayIndexOutOfBoundsException异常是由于试图检索一个不存在的“盒子”,通过传递一个比最后一个“盒子”的索引更高的索引,或者是负的索引而引起的。
在我的运行示例中,这些代码片段将产生这样一个异常:
myArray[5] //tries to retrieve the 6th "box" when there is only 5
myArray[-1] //just makes no sense
myArray[1337] //way to high
如何避免ArrayIndexOutOfBoundsException
为了防止ArrayIndexOutOfBoundsException,有一些关键点需要考虑:
循环
当循环遍历一个数组时,始终确保您正在检索的索引严格小于数组的长度(盒子的数量)。例如:
for (int i = 0; i < myArray.length; i++) {
注意<,不要把a =混进去。
你可能想做这样的事情:
for (int i = 1; i <= myArray.length; i++) {
final int someint = myArray[i - 1]
只是不喜欢。坚持上面的一个(如果你需要使用索引),它会为你省去很多痛苦。
如果可能,请使用foreach:
for (int value : myArray) {
这样你就完全不用考虑索引了。
在循环时,无论你做什么,永远不要改变循环迭代器的值(这里:i)。它唯一应该改变值的地方是让循环继续下去。以其他方式更改它只是冒着异常的风险,并且在大多数情况下是不必要的。
检索/更新
当检索数组的任意元素时,总是检查它是一个针对数组长度的有效索引:
public Integer getArrayElement(final int index) {
if (index < 0 || index >= myArray.length) {
return null; //although I would much prefer an actual exception being thrown when this happens.
}
return myArray[index];
}