When java.lang.ArrayIndexOutOfBoundsException occurs in java

What is hierarchy of java.lang.ArrayIndexOutOfBoundsException?

-java.lang.Object
-java.lang.Throwable
 -java.lang.Exception
  -java.lang.RuntimeException
   -java.lang.IndexOutOfBoundsException
    -java.lang.ArrayIndexOutOfBoundsException



ArrayIndexOutOfBoundsException is Checked (compile time exceptions) and UnChecked (RuntimeExceptions) in java ?

java.lang.ArrayIndexOutOfBoundsException is a RuntimeExceptions in java.


What is ArrayIndexOutOfBoundsException in java?

ArrayIndexOutOfBoundsException is thrown when an array is accessed with an illegal index.

The index accessed may be >

  • Negative or
  • equal to the size of array
  • greater than the size of array.



Scenarios where ArrayIndexOutOfBoundsException may be thrown in java>



Let’s take an example where using array can throw ArrayIndexOutOfBoundsException >

  1. If index accessed is Negative, ArrayIndexOutOfBoundsException is thrown
  2. If index accessed is equal to the size of array, ArrayIndexOutOfBoundsException is thrown
  3. If index accessed is greater than the size of array, ArrayIndexOutOfBoundsException is thrown

public class ArrayIndexOutOfBoundsExceptionExample {
public static void main(String args[]) {
     int ar[] = { 11, 22, 23, 31};
    
     //If index accessed is Negative, ArrayIndexOutOfBoundsException is thrown
     //System.out.println(ar[-1]);
     System.out.println(ar[0]); //11
     System.out.println(ar[1]); //22
     System.out.println(ar[2]); //23
     System.out.println(ar[3]); //31
    
     //If index accessed is equal to the size of array, ArrayIndexOutOfBoundsException is thrown
     //System.out.println(ar[4]);
    
     //If index accessed is greater than the size of array, ArrayIndexOutOfBoundsException is thrown
     //System.out.println(ar[5]);
}
}

If we uncomment any of the above comment line ArrayIndexOutOfBoundsException will be thrown >

System.out.println(ar[-1]);
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at ArrayIndexOutOfBoundsExceptionExample.main( ArrayIndexOutOfBoundsExceptionExample.java:6)


System.out.println(ar[4]);
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at ArrayIndexOutOfBoundsExceptionExample.main( ArrayIndexOutOfBoundsExceptionExample.java:14)


System.out.println(ar[5]);
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at ArrayIndexOutOfBoundsExceptionExample.main( ArrayIndexOutOfBoundsExceptionExample.java:17)





Let’s take an example where using java.util.ArrayList can throw ArrayIndexOutOfBoundsException >

  1. If index accessed is Negative, ArrayIndexOutOfBoundsException is thrown
  2. If index accessed is equal to the size of ArrayList, ArrayIndexOutOfBoundsException is thrown
  3. If index accessed is greater than the size of ArrayList, ArrayIndexOutOfBoundsException is thrown

import java.util.ArrayList;
import java.util.List;
public class ArrayIndexOutOfBoundsExceptionExample2 {
public static void main(String args[]) {
    
     List<Integer> arrayList = new ArrayList<Integer>();
     arrayList.add(11);
     arrayList.add(22);
     arrayList.add(23);
     arrayList.add(31);
    
     //If index accessed is Negative, ArrayIndexOutOfBoundsException is thrown
     //System.out.println(arrayList.get(-1));
     System.out.println(arrayList.get(0)); //11
     System.out.println(arrayList.get(1)); //22
     System.out.println(arrayList.get(2)); //23
     System.out.println(arrayList.get(3)); //31
    
     //If index accessed is equal to the size of array, ArrayIndexOutOfBoundsException is thrown
     //System.out.println(arrayList.get(4));
    
     //If index accessed is greater than the size of array, ArrayIndexOutOfBoundsException is thrown
     //System.out.println(arrayList.get(5));
}
}
/*OUTPUT
11
22
23
31
*/

If we uncomment any of the above comment line ArrayIndexOutOfBoundsException will be thrown >

System.out.println(arrayList.get(-1))
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at ArrayIndexOutOfBoundsExceptionExample2.main(ArrayIndexOutOfBoundsExceptionExample2.java:14)



System.out.println(arrayList.get(4))
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at ArrayIndexOutOfBoundsExceptionExample2.main(ArrayIndexOutOfBoundsExceptionExample2.java:22)



System.out.println(arrayList.get(5))
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 4
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at ArrayIndexOutOfBoundsExceptionExample2.main(ArrayIndexOutOfBoundsExceptionExample2.java:25)

No comments:

Post a Comment