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]);
}
当前回答
对于任何长度为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); }
其他回答
我在这里看到了所有解释如何使用数组以及如何避免索引越界异常的答案。我个人不惜一切代价避免数组。我使用Collections类,这避免了必须完全处理数组下标的所有愚蠢行为。循环构造与支持更容易编写、理解和维护的代码的集合完美地工作。
在代码中,您已经访问了从索引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');
}
ArrayIndexOutOfBounds表示您正在尝试索引数组中未分配的位置。
在这种情况下:
String[] name = { "tom", "dick", "harry" };
for (int i = 0; i <= name.length; i++) {
System.out.println(name[i]);
}
的名字。length是3,因为数组是用3个String对象定义的。 当访问数组的内容时,position从0开始。因为有3个项目,这意味着名字[0]=“tom”,名字[1]=“dick”,名字[2]=“harry” 当你循环时,因为i可以小于或等于name。长度,您正在尝试访问名称[3],该名称不可用。
为了解决这个问题…
In your for loop, you can do i < name.length. This would prevent looping to name[3] and would instead stop at name[2] for(int i = 0; i<name.length; i++) Use a for each loop String[] name = { "tom", "dick", "harry" }; for(String n : name) { System.out.println(n); } Use list.forEach(Consumer action) (requires Java8) String[] name = { "tom", "dick", "harry" }; Arrays.asList(name).forEach(System.out::println); Convert array to stream - this is a good option if you want to perform additional 'operations' to your array e.g. filter, transform the text, convert to a map etc (requires Java8) String[] name = { "tom", "dick", "harry" }; --- Arrays.asList(name).stream().forEach(System.out::println); --- Stream.of(name).forEach(System.out::println);
这个简单的问题就讲到这里,我只想强调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
}