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中的異常處理
ArrayIndexOutOfBoundsException super classes are Exception
, RuntimeException
and IndexOutOfBoundsException
.
java是什么、 ArrayIndexOutOfBoundsException超類是Exception
, RuntimeException
和IndexOutOfBoundsException
。
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.
java.lang.stackoverflowerror, 下面的日志顯示了以上程序的執行之一。
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: 4at 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。 還要注意,異常堆棧跟蹤會打印出導致異常的非法索引。
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.
java.lang.UnsatisfiedLinkError, 我們不應該嘗試從此異常中恢復,而應嘗試通過檢查傳遞的索引值是否為有效值來減輕這種異常。 還要注意的是,這種情況多半發生在用戶輸入的情況下,如果我們自己創建數組并對其元素進行迭代,則發生此異常的機會就會減少。
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 programsc.close(); //close resources}
}
Now the output will be like below when illegal index is passed.
java.util.arraylist cannot be、 現在,通過非法索引后,輸出將如下所示。
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
java.lang.NoClassDefFoundError、 參考: API文檔
翻譯自: https://www.journaldev.com/18092/java-lang-arrayindexoutofboundsexception
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态