You are currently viewing IndexOutOfBoundsException in Java

IndexOutOfBoundsException in Java

  • Post author:
  • Post category:Java
  • Post comments:0 Comments
  • Post last modified:May 29, 2024

What is IndexOutOfBoundsException?

IndexOutOfBoundsException is a runtime exception that occurs in Java when you try to access an element from an array, ArrayList, or any other collection type using an index that is either negative or greater than or equal to the size of the collection.

Causes of IndexOutOfBoundsException

  1. Accessing an Array Out of Bounds: Trying to access an element in an array using an index that is less than 0 or greater than or equal to the length of the array.
  2. Accessing an ArrayList Out of Bounds: Trying to access an element in an ArrayList using an index that is less than 0 or greater than or equal to the size of the ArrayList.

Example 1: Array IndexOutOfBoundsException

public class ArrayExample {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};

        // Accessing element at index 5 which is out of bounds
        System.out.println(numbers[5]); // This will throw IndexOutOfBoundsException
    }
}

In this example, the array numbers has a length of 5, so the valid indices are 0 to 4. Attempting to access numbers[5] will result in an IndexOutOfBoundsException because it is out of bounds.

Example 2: ArrayList IndexOutOfBoundsException

import java.util.ArrayList;

public class ArrayListExample {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");

        // Accessing element at index 2 which is out of bounds
        System.out.println(names.get(2)); // This will throw IndexOutOfBoundsException
    }
}

In this example, the ArrayList names has a size of 2 because it contains two elements. Attempting to access names.get(2) will result in an IndexOutOfBoundsException because it is out of bounds.

Handling IndexOutOfBoundsException

To handle IndexOutOfBoundsException, you can use exception handling mechanisms such as try-catch blocks:

public class ArrayExample {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};

        try {
            // Accessing element at index 5 which is out of bounds
            System.out.println(numbers[5]);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Index is out of bounds: " + e.getMessage());
        }
    }
}

Preventing IndexOutOfBoundsException

To prevent IndexOutOfBoundsException, always ensure that the index you’re accessing is within the bounds of the array or collection. You can use conditional statements or loops to check the index before accessing the element.

public class ArrayExample {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        int index = 5;

        if (index >= 0 && index < numbers.length) {
            System.out.println(numbers[index]);
        } else {
            System.out.println("Index is out of bounds");
        }
    }
}

Conclusion

IndexOutOfBoundsException is a common exception in Java that occurs when trying to access elements outside the valid index range of an array or collection. Understanding its causes, handling, and prevention methods is essential for writing robust Java programs.

Leave a Reply