I am new to Java and was running below code which runs fine but I get an array index out of bound exception. Can someone please help understand why am I getting this exception?
public class array {
public static void main (String[] args)
{
int[] b = {1,2,3,4};
array ar = new array();
ar.process(b);
}
public int process (int[] a)
{
int i;
System.out.println("Length is: " +a.length);
for(i = 0; i < a.length ; i++) {
System.out.println("A is : " + a[i] + " I is" +i);
}
return a[i];
}
}
Exception
Length is: 4
A is : 1 I is0
A is : 2 I is1
A is : 3 I is2
A is : 4 I is3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at array.process(array.java:17)
at array.main(array.java:7)