ArrayIndexOutOfBoundException and NegativeArraySizeException in Java

Phantasia1116發表於2024-03-21

ArrayIndexOutOfBoundException

ArrayIndexOutOfBoundsException occurs when we access an array, or a Collection, that is backed by an array with an invalid index. This means that the index is either less than zero or greater than or equal to the size of the array.

  • 也就是說,使用 index 索引陣列的時候可能出現了負數或者是大於陣列長度的數

可能出錯的情況

越界訪問陣列

Accessing the array elements out of these bounds would throw an ArrayIndexOutOfBoundsException:

int[] numbers = new int[] {1, 2, 3, 4, 5};
int lastNumber = numbers[5];

Here, the size of the array is 5, which means the index will range from 0 to 4.
In this case, accessing the 5th index results in an ArrayIndexOutOfBoundsException:

Here, the size of the array is 5, which means the index will range from 0 to 4.

In this case, accessing the 5th index results in an _ArrayIndexOutOfBoundsException_:
訪問由 Arrays.asList() 返回的 List

If we try to access the elements of the List returned by Arrays.asList() beyond this range, we would get an ArrayIndexOutOfBoundsException:

List<Integer> numbersList = Arrays.asList(1, 2, 3, 4, 5);
int lastNumber = numbersList.get(5);

Here again, we are trying to get the last element of the List. The position of the last element is 5, but its index is 4 (size – 1). Hence, we get ArrayIndexOutOfBoundsException as below:

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  ...
迴圈遍歷陣列

Sometimes, while iterating over an array in a for loop, we might put a wrong termination expression.

Instead of terminating the index at one less than the length of the array, we might end up iterating until its length:

int sum = 0;
for (int i = 0; i <= numbers.length; i++) {
    sum += numbers[i];
}

In the above termination expression, the loop variable i is being compared as less than or equal to the length of our existing array numbers. So, in the last iteration, the value of i will become 5.

Since index 5 is beyond the range of numbers, it will again lead to 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)

Avoid This Exception

  • Remembering the index
  • Correctly Using the Operators in Loops
  • Using Enhanced for Loops (using iterator)

NegativeArraySizeException

什麼導致了NegativeArraySizeException in Java

The NegativeArraySizeException occurs when an attempt is made to assign a negative size to an array. Here's an example:

public class NegativeArraySizeExceptionExample {
    public static void main(String[] args) {
        int[] array = new int[-5];
        System.out.println("Array length: " + array.length);
    }
}

Running the above code throws the following exception:

Exception in thread "main" java.lang.NegativeArraySizeException: -5
    at NegativeArraySizeExceptionExample.main(NegativeArraySizeExceptionExample.java:3)

怎麼解決 NegativeArraySizeException in Java

The NegativeArraySizeException can be handled in code using the following steps:

  • Surround the piece of code that can throw an NegativeArraySizeException in a try-catch block.
  • Catch the NegativeArraySizeException in the catch clause.
  • Take further action as necessary for handling the exception and making sure the program execution does not stop.

Here's an example of how to handle it in code:

public class NegativeArraySizeExceptionExample {
    public static void main(String[] args) {
        try {
            int[] array = new int[-5];
        } catch (NegativeArraySizeException nase) {
            nase.printStackTrace();
            //handle the exception
        }
        System.out.println("Continuing execution...");
    }
}

In the above example, the lines that throw the NegativeArraySizeException are placed within a try-catch block. The NegativeArraySizeException is caught in the catch clause and its stack trace is printed to the console. Any code that comes after the try-catch block continues its execution normally.

Running the above code produces the following output:

java.lang.NegativeArraySizeException: -5
    at NegativeArraySizeExceptionExample.main(NegativeArraySizeExceptionExample.java:4)
Continuing execution...

怎麼避免 NegativeArraySizeException in Java

Since the NegativeArraySizeException occurs when an array is created with a negative size, assigning a positive size to the array can help avoid the exception. Applying this to the earlier example helps fix the issue:

public class NegativeArraySizeExceptionExample {
    public static void main(String[] args) {
        int[] array = new int[5];
        System.out.println("Array length: " + array.length);
    }
}

The array is initialized with a size of 5, which is a positive number. Running the above code produces the correct output as expected:

Array length: 5

reference:

  • [JAVA ArrayIndexOutOfBoundsException][https://www.baeldung.com/java-arrayindexoutofboundsexception]
  • [NegativeArraySizeException][https://rollbar.com/blog/java-negativearraysizeexception/]

相關文章