title: Populating a two-dimensional arrays in Java
body: <p>So this question is more theoretical since I am just starting to learn the Java language. It might sound very basic but I wanted to ask it anyway.</p>
  
  <p>This is the code of my two-dimensional array. </p>
  
  <pre><code>int matrix[][] = new int[5][];
  for (int i = 0; i &lt; matrix.length; i++){
          matrix[i] = new int[i];
  
  
              for(int j = 0; j &lt; matrix[i].length; j++){
                  matrix[i][j] = i;
              }
          }
  </code></pre>
  
  <p>The first loop sets the number of elements of each inner array.</p>
  
  <p>When I assign "i" for the matrix[ i ][ j ], the result would be:</p>
  
  <pre><code>matrix[0] is &lt;&gt;
  matrix[1] is &lt;1&gt;
  matrix[2] is &lt;2, 2&gt;
  matrix[3] is &lt;3, 3, 3&gt;
  matrix[4] is &lt;4, 4, 4, 4&gt; 
  </code></pre>
  
  <p>And the result for "j" in the same code: </p>
  
  <pre><code>for(int j = 0; j &lt; matrix[i].length; j++){
                  matrix[i][j] = j;
              }
  </code></pre>
  
  <p>would be: </p>
  
  <pre><code>matrix[0] is &lt;&gt;
  matrix[1] is &lt;0&gt;
  matrix[2] is &lt;0, 1&gt;
  matrix[3] is &lt;0, 1, 2&gt;
  matrix[4] is &lt;0, 1, 2, 3&gt;
  </code></pre>
  
  <p>So I try to understand as to why when I use the "i" for populating the arrays I have repeating numbers in the arrays, while I use the value of "j" it yields values starting from 0, 1, 2... </p>
  
  <p>So would you please explain it to me step by step how for loops are iterating the arrays, the concept of iterating the arrays? I would really appreciate the given help! Thank you!</p>