I am simply trying to iterate through every variable in an array of arrays:
Here's the working code:
int[][] mArray;
mArray = new int[2][2];
mArray[0][0] = 1;
mArray[0][1] = 2;
mArray[1][0] = 3;
mArray[1][1] = 4;
for (int i = 0; i < mArray.length; i++)
{
for (int x = 0; x < mArray[i].length; x++)
{
System.out.println(mArray[i][x]);
}
}
This prints out:
1 2 3 4
So Everything's fine.
However if I replace
"for (int x = 0; x < mArray[i].length; x++)"
with
"for (int x = 0; x < mArray[x].length; x++)"
It get the following error: "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2".
Can anyone explain why this error happens to occur? Both mArray[i].length and mArray[x].length result in a value of "2", so why does the second option not work?
Thanks in advance :)