ArrayIndexOutOfBounds Vs ArrayStoreException in Java?


An array is a data structure/container/object that stores a fixed-size sequential collection of elements of the same type. The size/length of the array is determined at the time of creation.

The position of the elements in the array is called as index or subscript. The first element of the array is stored at the index 0 and, the second element is at the index 1 and so on.

Creating an array

In Java, arrays are treated as referenced types you can create an array using the new keyword similar to objects and populate it using the indices as −

int myArray[] = new int[7];
myArray[0] = 1254;
myArray[1] = 1458;
myArray[2] = 5687;
myArray[3] = 1457;
myArray[4] = 4554;
myArray[5] = 5445;
myArray[6] = 7524;

Or, you can directly assign values with in flower braces separating them with commas (,) as −

int myArray = {1254, 1458, 5687, 1457, 4554, 5445, 7524};

Accessing an element in an array

Each element in an array is accessed using an expression which contains the name of the array followed by the index of the required element in square brackets.

System.out.println(myArray[3]);
//prints 1457

ArrayIndexOutOfBoundsException

Generally, an array is of fixed size and each element is accessed using the indices. For example, we have created an array with size 7. Then the valid expressions to access the elements of this array will be a[0] to a[6] (length-1).

Whenever, you used an –ve value or, the value greater than or equal to the size of the array, then the ArrayIndexOutOfBoundsException is thrown.

For Example, if you execute the following code, it displays the elements in the array asks you to give the index to select an element. Since the size of the array is 7, the valid index will be 0 to 6.

Example

import java.util.Arrays;
import java.util.Scanner;
public class AIOBSample {
   public static void main(String args[]){
      int[] myArray = {1254, 1458, 5687,1457, 4554, 5445, 7524};
      System.out.println("Elements in the array are: ");
      System.out.println(Arrays.toString(myArray));
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the index of the required element: ");
      int element = sc.nextInt();
      System.out.println("Element in the given index is :: "+myArray[element]);
   }
}

But if you observe the below output we have requested the element with the index 9 since it is an invalid index an ArrayIndexOutOfBoundsException raised and the execution terminated.

Output

Elements in the array are:
[897, 56, 78, 90, 12, 123, 75]
Enter the index of the required element:
7
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at AIOBSample.main(AIOBSample.java:12)

ArrayStoreException

When you have created an array of a particular datatype with fixed size and populating it, if you store a value other than its datatype an ArrayStoreException is thrown at the run time.

Example

In the following Java program, we are creating an Integer array and trying to store a double value in it.

import java.util.Arrays;
public class ArrayStoreExceptionExample {
   public static void main(String args[]) {
      Number integerArray[] = new Integer[3];
      integerArray[0] = 12548;
      integerArray[1] = 36987;
      integerArray[2] = 555.50;
      integerArray[3] = 12548;
      System.out.println(Arrays.toString(integerArray));
   }
}

Runtime Exception

This program gets compiled successfully but, while executing it an ArrayStoreException is thrown.

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Double
at ther.ArrayStoreExceptionExample.main(ArrayStoreExceptionExample.java:9)

Updated on: 02-Aug-2019

262 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements