Check if an index exists in Java array
This post will discuss how to check if an index exists in Java array.
The ArrayIndexOutOfBoundsException is thrown whenever we’re trying to access an array index that is either negative or greater than or equal to the size of the array. For instance, the following code tries to access an invalid array index and results in an ArrayIndexOutOfBoundsException.
|
1 2 3 4 5 6 7 8 9 |
public class Main { public static void main(String[] args) { int[] arr = {4, 3, 2, 3, 1}; int index = 6; System.out.println("The element at index " + index + " is " + arr[index]); } } |
Output:
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 5
at Main.main(Main.java:7)
To avoid getting an ArrayIndexOutOfBoundsException, we can explicitly check if the array index is within the bounds of the array before accessing it.
1. Custom routine to check length
A simple solution is to write a custom routine to check if an array index is valid or not. An array index is valid if it is non-negative and less than the size of the array. The array’s length can be determined from the final instance variable length.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class Main { public static boolean isValidIndex(int[] arr, int index) { return index >= 0 && index < arr.length; } public static void main(String[] args) { int[] arr = {4, 3, 2, 3, 1}; int index = 6; if (isValidIndex(arr, index)) { System.out.println("The element at index " + index + " is " + arr[index]); } else { System.out.println("Index " + index + " out of bounds for length " + arr.length); } } } |
Output:
Index 6 out of bounds for length 5
2. Using try-catch block
The idea here is to try accessing the array within a try-catch block. Return false if the code throws ArrayIndexOutOfBoundsException, true otherwise. The array can be accessed using an indexing expression, enclosed by [ and ].
Here’s the complete code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class Main { public static boolean isValidIndex(int[] arr, int index) { try { int i = arr[index]; } catch (IndexOutOfBoundsException e) { return false; } return true; } public static void main(String[] args) { int[] arr = {4, 3, 2, 3, 1}; int index = 6; if (isValidIndex(arr, index)) { System.out.println("The element at index " + index + " is " + arr[index]); } else { System.out.println("Index " + index + " out of bounds for length " + arr.length); } } } |
Output:
Index 6 out of bounds for length 5
3. Using Objects.checkIndex() method
Since Java 9, we can check if the index is within the bounds of the specified range using the Objects.checkIndex() method. It returns the index if it is within bounds of the range and throws IndexOutOfBoundsException if the index is out of bounds. It can be used as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Objects; public class Main { public static boolean isValidIndex(int[] arr, int index) { try { Objects.checkIndex(index, arr.length); } catch (IndexOutOfBoundsException e) { return false; } return true; } public static void main(String[] args) { int[] arr = {4, 3, 2, 3, 1}; int index = 6; if (isValidIndex(arr, index)) { System.out.println("The element at index " + index + " is " + arr[index]); } else { System.out.println("Index " + index + " out of bounds for length " + arr.length); } } } |
Output:
Index 6 out of bounds for length 5
That’s all about checking if an index exists in Java array.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)