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]);
}
当前回答
对于给定的数组,数组的长度是3。的名字。但是因为它存储的是从索引0开始的元素,所以它的最大索引是2。
因此,代替'i**<=name。长度'你应该写'i<**name。长度',以避免'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];
}
对于多维数组,要确保访问正确维度的length属性可能有些棘手。以下面的代码为例:
int [][][] a = new int [2][3][4];
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[i].length; j++){
for(int k = 0; k < a[j].length; k++){
System.out.print(a[i][j][k]);
}
System.out.println();
}
System.out.println();
}
每个维度都有不同的长度,因此,中间循环和内部循环使用相同维度的length属性(因为a[i]。Length与a[j]. Length相同)。
相反,内部循环应该使用[i][j]。长度(或[0][0]。长度,为了简单)。
你的第一个目标应该是能够合理清晰地解释它的文档:
抛出,表示使用非法索引访问了数组。索引值为负或大于或等于数组的大小。
例如:
int[] array = new int[5];
int boom = array[10]; // Throws the exception
至于如何避免……嗯,别这么做。小心你的数组索引。
人们有时会遇到的一个问题是认为数组是1索引的,例如。
int[] array = new int[5];
// ... populate the array here ...
for (int index = 1; index <= array.length; index++)
{
System.out.println(array[index]);
}
这将遗漏第一个元素(索引0),并在索引为5时抛出异常。这里的有效索引是0-4。正确的,地道的for语句应该是:
for (int index = 0; index < array.length; index++)
(当然,这是假设您需要索引。如果你可以使用增强的for循环,那就这样做。)
对于任何长度为n的数组,数组元素的索引将从0到n-1。
如果您的程序试图访问数组索引大于n-1的任何元素(或内存),则Java将抛出ArrayIndexOutOfBoundsException
这里有两种我们可以在程序中使用的解决方案
保持数: For (int count = 0;计数< array.length;计数+ +){ System.out.println(阵列[数]); } 或者其他循环语句 Int count = 0; While (count < array.length) { System.out.println(阵列[数]); 数+ +; } 一个更好的方法是使用for循环,在这种方法中,程序员不需要担心数组中元素的数量。 for(String str: array) { System.out.println (str); }
由于i<=name,你会得到ArrayIndexOutOfBoundsException。长度的部分。的名字。Length返回字符串名称的长度,即3。因此,当您试图访问名称[3]时,它是非法的,并抛出异常。
解决代码:
String[] name = {"tom", "dick", "harry"};
for(int i = 0; i < name.length; i++) { //use < insteadof <=
System.out.print(name[i] +'\n');
}
它在Java语言规范中定义:
公共final字段长度,它包含组件的数量 数组的。长度可以为正或零。