In this tutorial, we will discuss what a Java ArrayIndexOutOfBoundsException is, what causes it and how to make sure that you avoid it.

If you fancy an assistant on your desk to entertain you, tell you about the weather or your appointments, then I recommend the Anki Cozmo – Collector’s Edition robot (click to check the price on Amazon). The Anki Cozmo will entertain you during those boring or lonely moments at work, or while studying, and will help you to keep your schedule and be more alert throughout the day.

What is an ArrayIndexOutOfBoundsException

An array-index out of bounds exception is a Java exception thrown due to the fact that the program is trying to access an element at a position that is outside an array’s limits or boundaries, hence the words “Out of bounds”. In other words, the program is trying to access an element at an index that is outside the array bounds. To understand array bounds, let us consider the following diagram

The picture above contains an array that consists of 7 elements. Each element in the array has its own index/position. In Java and lots of other programming languages, an index always starts with 0 and ends with the number of elements in the array -1. For example, the array above consists of 7 elements, therefore it’s indices start from 0 and end with 6 (7-1). Attempting o access an element with an index less than 0 or more than 6 will cause Java to throw an ArrayIndexOutOfBoundsException. Now that we know the causes of an out of bounds exception, let us discuss some common mistakes that people fall into that cause the exception.

Accessing elements within a loop with the loop counter as the index

An operation that is often performed in programming is accessing a section of, or the entire array, within a loop. While doing so, one must take precautions as not to go out of the bounds of the array. For example, when using a loop counter as an index, a common mistake is to define the bounds from the counter with a value of 0 to the counter <= the length of array, as in the following example.


Running the program will yield the following output.


We received the above output because of the loop condition “i <= array.length”. First of all, array.length represents the number of elements in the array and not the last index value in the array. Since the array has six elements, it’s index bounds are from 0 to 5. However, the loop condition will stay true even when the counter i is equal to 6. This is because array.length is 6. Therefore, make sure when looping over elements to know the range of your counter. The following piece of code is a fix to the previous one.


You can also use a foreach loop, which will eliminate the need of an index or a counter. This can also make your code more readable, specially when writing nested loops. We can convert the code above into a foreach loop as follows.



Accessing 2D/multi-dimension array elements

Another common mistake new and beginner programmers make is to use the wrong index when accessing 2D and multi-dimension array elements. Take for example the following piece of code.


The above code will cause an ArrayIndexOutOfBoundsException to be thrown at the fourth print statement. This is because the statements tried to access the wrong array/row. The 2D array array2D consists of 3 rows and 4 columns. Therefore, in order to access a certain element within the array, we would first have to access the row, and then the column. Since a 2D array is an array of arrays, we can consider each row as an individual array. To access a row, we use “array2D[row]”. To access an element within a row, we use an index within that row, hence we use the statement “array2D[row][column]”. Obviously, the program above has flipped the indices and will cause an exception to be thrown.


To fix the program above, we simply have to flip the indices as follows.



Summary

In this tutorial, we discussed some common mistakes that take place when dealing with arrays and why we could get an ArrayOutOfBoundsException.

If you would like to improve your knowledge about Java and how to write high performance code like a pro, then I would recommend reading Java Performance: The Definitive Guide: Getting the Most Out of Your Code (click to check current price on Amazon).

If you liked this content, then make sure to follow us on twitter to be up to date with the latest posts and Java tutorials.