I am learning Java's Multidimensional arrays. When I set arr ={{1,2,3},{4,5,6}}
and int x = arr[2 or more][any digit]
, the ArrayIndexOutOfBoundsException
comes out.
public class Array {
public static void main(String[] args) {
int [][] arr= { {4,5,6,7},{1,2,3,8}};
int x;
for(int a= 0,b= 0;a<= 3 && b<= 3; a++, b++){
try {
x = arr[a][b];
System.out.println("a = "+ a + " b = "+ b +"\n"+ x +"\nCorrect----------------------");
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("a = "+ a + " b = "+ b +"\nERROR------------------");
}
}
}
}
Result:
a = 0 b = 0
4
Correct----------------------
a = 1 b = 1
2
Correct----------------------
a = 2 b = 2
ERROR------------------
a = 3 b = 3
ERROR------------------