Java ArrayIndexOutOfBoundsException異常
- java
一、概述
在本教程中,我們將討論 Java 中的ArrayIndexOutOfBoundsException
。我們將了解它為什麼會發生以及如何避免它。
2. ArrayIndexOutOfBoundsException
什麼時候發生?
眾所周知,在Java中,數組是一種靜態數據結構,我們在創建時就定義了它的大小。
我們使用索引訪問數組的元素。數組中的索引從零開始,並且不能大於或等於數組的大小。
簡而言之,經驗法則是 0 <= index <(數組大小)。
ArrayIndexOutOfBoundsException
在我們訪問一個數組或Collection
時發生,該數組由具有無效索引的數組支持。這意味著索引小於零或大於或等於數組的大小。
此外,邊界檢查發生在運行時。因此, ArrayIndexOutOfBoundsException
是一個運行時異常。因此,我們在訪問數組的邊界元素時需要格外小心。
讓我們了解一些導致ArrayIndexOutOfBoundsException
的常見操作。
2.1 訪問數組
訪問數組時可能發生的最常見錯誤是忘記了上限和下限。
數組的下限始終為 0,而上限比其長度小 1。
訪問超出這些範圍的數組元素會拋出ArrayIndexOutOfBoundsException
:
int[] numbers = new int[] {1, 2, 3, 4, 5};
int lastNumber = numbers[5];
這裡,數組的大小是 5,這意味著索引的範圍是 0 到 4。
在這種情況下,訪問第 5 個索引會導致ArrayIndexOutOfBoundsException
:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at ...
同樣,如果我們將小於零的值作為數字的索引傳遞,我們會得到ArrayIndexOutOfBoundsException
numbers.
2.2.訪問Arrays.asList()
返回的List
靜態方法Arrays.asList()
返回一個由指定數組支持的固定大小的列表。此外,它充當基於數組和基於集合的 API 之間的橋樑。
返回的List
具有基於索引訪問其元素的方法。此外,與數組類似,索引從零開始,範圍為比其大小小一。
如果我們嘗試訪問超出此範圍的Arrays.asList()
返回的List
的元素,我們會得到一個ArrayIndexOutOfBoundsException
:
List<Integer> numbersList = Arrays.asList(1, 2, 3, 4, 5);
int lastNumber = numbersList.get(5);
在這裡,我們再次嘗試獲取List
的最後一個元素。最後一個元素的位置是 5,但它的索引是 4(大小 - 1)。因此,我們得到ArrayIndexOutOfBoundsException
如下:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at java.base/java.util.Arrays$ArrayList.get(Arrays.java:4351)
at ...
同樣,如果我們傳遞一個負索引,比如 -1,我們將得到類似的結果。
2.3.在循環中迭代
有時,在 for 循環中迭代數組時,我們可能會輸入錯誤的終止表達式。
我們可能最終迭代直到它的長度,而不是在比數組長度小一的位置終止索引:
int sum = 0;
for (int i = 0; i <= numbers.length; i++) {
sum += numbers[i];
}
在上面的終止表達式中,循環變量i
被比較為小於或等於我們現有數組numbers.
因此,在最後一次迭代中, i
的值將變為 5。
由於索引 5 超出了numbers,
它將再次導致ArrayIndexOutOfBoundsException
:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at com.baeldung.concatenate.IndexOutOfBoundExceptionExamples.main(IndexOutOfBoundExceptionExamples.java:22)
3. 如何避免ArrayIndexOutOfBoundsException
?
現在讓我們了解一些避免ArrayIndexOutOfBoundsException
的方法。
3.1 記住起始索引
我們必須始終記住,數組索引在 Java 中從 0 開始。因此,第一個元素始終位於索引 0 處,而最後一個元素位於比數組長度小 1 處的索引處。
記住這條規則將幫助我們在大多數情況下避免ArrayIndexOutOfBoundsException
。
3.2.正確使用循環中的運算符
將循環變量錯誤地初始化為索引 1 可能會導致ArrayIndexOutOfBoundsException
。
同樣,在循環的終止表達式中錯誤地使用運算符 <、<=、> 或 >= 是發生此異常的常見原因。
我們應該正確地確定這些運算符在循環中的使用。
3.3.使用增強for
循環
如果我們的應用程序在 Java 1.5 或更高版本上運行,我們應該使用增強for
循環語句,該語句專門開髮用於迭代集合和數組。此外,它使我們的循環更加簡潔易讀。
此外,使用增強for
循環可以幫助我們完全避免ArrayIndexOutOfBoundsException
,因為它不涉及索引變量:
for (int number : numbers) {
sum += number;
}
在這裡,我們不必擔心索引。增強for
循環在每次迭代中拾取一個元素並將其分配給循環變量number
。因此,它完全避免了ArrayIndexOutOfBoundsException
。
4. IndexOutOfBoundsException
與ArrayIndexOutOfBoundsException
當我們嘗試訪問超出其範圍的某種類型( String
、數組、 List
等)的索引時,會發生IndexOutOfBoundsException
。它是ArrayIndexOutOfBoundsException
和StringIndexOutOfBoundsException
的超類。
與ArrayIndexOutOfBoundsException
類似,當我們嘗試訪問具有超出其長度的索引的String
的字符時,會拋出StringIndexOutOfBoundsException
。
5. 結論
在本文中,我們探討了ArrayIndexOutOfBoundsException
,一些關於它如何發生的示例,以及一些避免它的常用技術。