java.lang.ArrayIndexOutOfBoundsException

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

ArrayIndexOutOfBoundsException超类是ExceptionRuntimeExceptionIndexOutOfBoundsException

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值