Aar*_*ron 273 java arrays indexoutofboundsexception
什么ArrayIndexOutOfBoundsException意思,我该如何摆脱它?
以下是触发异常的代码示例:
String[] name = { "tom", "dick", "harry" };
for (int i = 0; i <= name.length; i++) {
System.out.println(name[i]);
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 274
您的第一个停靠点应该是合理清楚地解释它的文档:
抛出以指示已使用非法索引访问数组.索引为负数或大于或等于数组的大小.
例如:
int[] array = new int[5];
int boom = array[10]; // Throws the exception
Run Code Online (Sandbox Code Playgroud)
至于如何避免......嗯,不要这样做.小心你的数组索引.
人们有时遇到的一个问题是认为数组是1索引的,例如
int[] array = new int[5];
// ... populate the array here ...
for (int index = 1; index <= array.length; index++)
{
System.out.println(array[index]);
}
Run Code Online (Sandbox Code Playgroud)
这将错过第一个元素(索引0)并在索引为5时抛出异常.此处的有效索引为0-4(含).这里正确的惯用语for是:
for (int index = 0; index < array.length; index++)
Run Code Online (Sandbox Code Playgroud)
(当然,假设您需要索引.如果您可以使用增强型for循环,请执行此操作.)
Bal*_*usC 48
if (index < 0 || index >= array.length) {
// Don't use this index. This is out of bounds (borders, limits, whatever).
} else {
// Yes, you can safely use this index. The index is present in the array.
Object element = array[index];
}
Run Code Online (Sandbox Code Playgroud)
更新:根据您的代码段,
for (int i = 0; i<=name.length; i++) {
Run Code Online (Sandbox Code Playgroud)
索引包含数组的长度.这是出界的.您需要替换<=的<.
for (int i = 0; i < name.length; i++) {
Run Code Online (Sandbox Code Playgroud)
aio*_*obe 20
简而言之:
在最后一次迭代中
for (int i = 0; i <= name.length; i++) {
Run Code Online (Sandbox Code Playgroud)
i将等于name.length哪个是非法索引,因为数组索引是从零开始的.
你的代码应该阅读
for (int i = 0; i < name.length; i++)
^
Run Code Online (Sandbox Code Playgroud)
Oct*_*ean 16
这意味着您正在尝试访问一个无效的数组索引,因为它不在边界之间.
例如,这将初始化具有上限4的原始整数数组.
int intArray[] = new int[5];
Run Code Online (Sandbox Code Playgroud)
程序员从零开始计算.因此,例如,这将抛出一个,ArrayIndexOutOfBoundsException因为上限是4而不是5.
intArray[5];
Run Code Online (Sandbox Code Playgroud)
Mak*_*oto 10
为了避免数组索引越界异常,应该在何时何地使用增强for语句.
主要动机(和用例)是在迭代时,您不需要任何复杂的迭代步骤.您将无法使用增强型for- 在数组中向后移动或仅在每个其他元素上进行迭代.
保证在执行此操作时不会耗尽元素进行迭代,并且您的[更正]示例很容易转换.
代码如下:
String[] name = {"tom", "dick", "harry"};
for(int i = 0; i< name.length; i++) {
System.out.print(name[i] + "\n");
}
Run Code Online (Sandbox Code Playgroud)
......等同于:
String[] name = {"tom", "dick", "harry"};
for(String firstName : name) {
System.out.println(firstName + "\n");
}
Run Code Online (Sandbox Code Playgroud)
Tob*_*obb 10
是什么原因ArrayIndexOutOfBoundsException?
如果您将变量视为可以放置值的"框",则数组是一系列相邻的框,其中框的数量是有限的显式整数.
像这样创建一个数组:
final int[] myArray = new int[5]
Run Code Online (Sandbox Code Playgroud)
创建一排5个盒子,每个盒子都有一个int.每个方框都有一个索引,一系列方框中的位置.该索引从0开始,以N-1结束,其中N是数组的大小(方框数).
要从此系列框中检索其中一个值,您可以通过其索引引用它,如下所示:
myArray[3]
Run Code Online (Sandbox Code Playgroud)
这将为您提供系列中第4个框的值(因为第一个框的索引为0).
ArrayIndexOutOfBoundsException通过传递高于最后一个"box"的索引的索引或负数来尝试检索不存在的"框"而导致An .
使用我的运行示例,这些代码片段会产生这样的异常:
myArray[5] //tries to retrieve the 6th "box" when there is only 5
myArray[-1] //just makes no sense
myArray[1337] //waay to high
Run Code Online (Sandbox Code Playgroud)
怎么避免 ArrayIndexOutOfBoundsException
为了防止ArrayIndexOutOfBoundsException,需要考虑以下几个要点:
循环
循环遍历数组时,请始终确保要检索的索引严格小于数组的长度(框数).例如:
for (int i = 0; i < myArray.length; i++) {
Run Code Online (Sandbox Code Playgroud)
请注意<,永远不要混在一起=..
你可能想要做这样的事情:
for (int i = 1; i <= myArray.length; i++) {
final int someint = myArray[i - 1]
Run Code Online (Sandbox Code Playgroud)
只是不要.坚持上面的那个(如果你需要使用索引)它会为你节省很多痛苦.
尽可能使用foreach:
for (int value : myArray) {
Run Code Online (Sandbox Code Playgroud)
这样您就不必考虑索引了.
在循环时,无论你做什么,都不要改变循环迭代器的值(这里:) 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];
}
Run Code Online (Sandbox Code Playgroud)
小智 6
对于给定的数组,数组的长度为 3(即 name.length = 3)。但是当它存储从索引 0 开始的元素时,它的最大索引为 2。
所以,你应该写 'i <**name.length' 来避免 'ArrayIndexOutOfBoundsException' ,而不是 'i**<= name.length '。
在您的代码中,您已经访问了从索引0到字符串数组长度的元素。name.length给出字符串对象数组中字符串对象的数量,即3,但是您最多只能访问索引2 name[2],因为可以从索引0到name.length - 1获取name.length对象数量的位置访问该数组。
即使在使用for循环时,您也从索引0开始,应该以结束name.length - 1。在数组a [n]中,您可以访问形式a [0]到a [n-1]。
例如:
String[] a={"str1", "str2", "str3" ..., "strn"};
for(int i=0;i<a.length()i++)
System.out.println(a[i]);
Run Code Online (Sandbox Code Playgroud)
在您的情况下:
String[] name = {"tom", "dick", "harry"};
for(int i = 0; i<=name.length; i++) {
System.out.print(name[i] +'\n');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
591629 次 |
| 最近记录: |