Iterating through an array of arrays in java : <p>I am simply trying to iterate through every variable in an array of arrays:</p>

<p>Here's the working code:</p>

<pre><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 &lt; mArray.length; i++)
    {
         for (int x = 0; x &lt; mArray[i].length; x++)
         {
            System.out.println(mArray[i][x]);
         }
    }    
</code></pre>

<p>This prints out:</p>

<p>1
2
3
4</p>

<p>So Everything's fine.</p>

<p>However if I replace</p>

<p>"for (int x = 0; x &lt; <strong>mArray[i].length</strong>; x++)"</p>

<p>with</p>

<p>"for (int x = 0; x &lt; <strong>mArray[x].length</strong>; x++)"</p>

<p>It get the following error: "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2".</p>

<p>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?</p>

<p>Thanks in advance :)</p>
