java.lang.ArrayIndexOutOfBoundsException is a runtime exception, so it’s a unchecked exception and don’t need to be thrown explicitly from method. Refer Exception Handling in java
java.lang.ArrayIndexOutOfBoundsException是运行时异常,因此它是未经检查的异常,不需要从方法中显式抛出。 请参阅Java中的异常处理
java.lang.ArrayIndexOutOfBoundsException (java.lang.ArrayIndexOutOfBoundsException)
- ArrayIndexOutOfBoundsException is thrown to indicate that we are trying to access array element with an illegal index. 引发ArrayIndexOutOfBoundsException表示我们正在尝试使用非法索引访问数组元素。
- This exception is thrown when the index is either negative or greater than or equal to the size of the array. 当索引为负数或大于或等于数组的大小时,抛出此异常。
ArrayIndexOutOfBoundsException类图 (ArrayIndexOutOfBoundsException Class Diagram)
ArrayIndexOutOfBoundsException super classes are Exception
, RuntimeException
and IndexOutOfBoundsException
.
![ArrayIndexOutOfBoundsException class diagram](https://i-blog.csdnimg.cn/blog_migrate/aa58e34e963342fa6198f78d38779da1.png)
ArrayIndexOutOfBoundsException超类是Exception
, RuntimeException
和IndexOutOfBoundsException
。
java.lang.ArrayIndexOutOfBoundsException示例 (java.lang.ArrayIndexOutOfBoundsException Example)
Let’s look at a simple example where our program may throw ArrayIndexOutOfBoundsException based on user input.
让我们看一个简单的示例,其中我们的程序可能根据用户输入抛出ArrayIndexOutOfBoundsException。
package com.journaldev.exceptions;
import java.util.Scanner;
public class ArrayIndexOutOfBoundsExceptionExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of int array:");
int size = sc.nextInt();
int[] intArray = new int[size];
for (int i = 0; i < size; i++) {
System.out.println("Please enter int value at index " + i + ":");
intArray[i] = sc.nextInt();
}
System.out.println("Enter array index to get the value:");
int index = sc.nextInt();
sc.close();
System.out.println("Value at " + index + " = " + intArray[index]);
}
}
Below log shows one of the execution of above program.
下面的日志显示了以上程序的执行之一。
Enter size of int array:
3
Please enter int value at index 0:
1
Please enter int value at index 1:
2
Please enter int value at index 2:
3
Enter array index to get the value:
4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at com.journaldev.exceptions.ArrayIndexOutOfBoundsExceptionExample.main(ArrayIndexOutOfBoundsExceptionExample.java:23)
So our program can throw ArrayIndexOutOfBoundsException because of illegal input from user. Also notice that exception stack trace prints the illegal index causing the exception.
因此,由于用户的非法输入,我们的程序可能会抛出ArrayIndexOutOfBoundsException。 还要注意,异常堆栈跟踪会打印出导致异常的非法索引。
如何修复ArrayIndexOutOfBoundsException (How to fix ArrayIndexOutOfBoundsException)
We shouldn’t try to recover from this exception, we should try to mitigate it by checking if the index value passed is a valid value or not. Also note that this situation can occur mostly in case of user input, if we are creating array ourself and iterating over it’s elements then chances of this exception are less.
我们不应该尝试从此异常中恢复,而应尝试通过检查传递的索引值是否为有效值来减轻这种异常。 还要注意的是,这种情况多半发生在用户输入的情况下,如果我们自己创建数组并对其元素进行迭代,则发生此异常的机会就会减少。
Below code snippet shows the small change in program while taking user input to access the element, if value passed is invalid then a warning message is shown to pass the valid value.
下面的代码片段显示了在使用用户输入来访问元素时程序中的微小变化,如果传递的值无效,则会显示警告消息以传递有效的值。
boolean exit = false;
while (!exit) {
System.out.println("Enter array index to get the value:");
int index = sc.nextInt();
if (index < 0 || index >= size) {
System.out.println("Valid index range is from 0 to " + (size - 1));
} else {
System.out.println("Value at " + index + " = " + intArray[index]);
exit = true; //to terminate the program
sc.close(); //close resources
}
}
Now the output will be like below when illegal index is passed.
现在,通过非法索引后,输出将如下所示。
Enter size of int array:
3
Please enter int value at index 0:
1
Please enter int value at index 1:
2
Please enter int value at index 2:
3
Enter array index to get the value:
4
Valid index range is from 0 to 2
Enter array index to get the value:
-1
Valid index range is from 0 to 2
Enter array index to get the value:
2
Value at 2 = 3
So this is how we avoid getting ArrayIndexOutOfBoundsException in our program. That’s all for a quick roundup on java.lang.ArrayIndexOutOfBoundsException
.
因此,这就是我们避免在程序中获取ArrayIndexOutOfBoundsException的方法。 这就是对java.lang.ArrayIndexOutOfBoundsException
的快速总结。
Reference: API Doc
参考: API文档
翻译自: https://www.journaldev.com/18092/java-lang-arrayindexoutofboundsexception