java是什么,java.lang.ArrayIndexOutOfBoundsException

 2023-11-19 阅读 22 评论 0

摘要: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.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

java是什么、 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.

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。 還要注意,異常堆棧跟蹤會打印出導致異常的非法索引。

如何修復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.

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

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/183157.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息