Contents

Java中ArrayIndexoutofBoundSexception简介

1. 概述

在本教程中,我们将讨论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

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被比较为小于或等于我们现有数组编号的长度。因此,在最后一次迭代中,i的值将变为 5。

由于索引 5 超出了数字范围,它将再次导致ArrayIndexOutOfBoundsException

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
    at com.blogdemo.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. IndexOutOfBoundsExceptionArrayIndexOutOfBoundsException

当我们尝试访问超出其范围的某种类型(stringarraylist等)的索引时,会发生IndexOutOfBoundsException。它是ArrayIndexOutOfBoundsExceptionStringIndexOutOfBoundsException的超类。

ArrayIndexOutOfBoundsException类似,当我们尝试访问具有超出其长度的索引的string的字符时,会抛出StringIndexOutOfBoundsException