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]);
}
当前回答
在大多数编程语言中,索引都是从0开始的。所以你必须写i<names。长度或i<=names。Length-1代替i<=names.length。
其他回答
在代码中,您已经访问了从索引0到字符串数组长度的元素。的名字。Length给出了字符串对象数组中字符串对象的数量,即3,但你只能访问到索引2名称[2], 因为可以从索引0到name访问数组。长度- 1,你得到名字。对象的长度。
即使在使用for循环时,您已经从索引0开始,并且应该以name结束。长度- 1。在数组a[n]中,您可以从[0]访问到a[n-1]。
例如:
String[] a={"str1", "str2", "str3" ..., "strn"};
for(int i=0; i<a.length(); i++)
System.out.println(a[i]);
在你的情况下:
String[] name = {"tom", "dick", "harry"};
for(int i = 0; i<=name.length; i++) {
System.out.print(name[i] +'\n');
}
这意味着您正在尝试访问一个数组的索引,该索引是无效的,因为它不在边界之间。
例如,这将初始化一个上限为4的基元整数数组。
int intArray[] = new int[5];
程序员从零开始计数。例如,这个会抛出一个ArrayIndexOutOfBoundsException,因为上限是4而不是5。
intArray[5];
此错误发生在运行循环超过限制次数时。让我们考虑一个简单的例子,
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。我希望你能明白。谢谢你!
你的第一个目标应该是能够合理清晰地解释它的文档:
抛出,表示使用非法索引访问了数组。索引值为负或大于或等于数组的大小。
例如:
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循环,那就这样做。)
什么原因导致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];
}