title: JAVA Writing the diagonal of an 2-dimensional array into an 1-dimensional array
body: <p>Want to write the diagonal of an 2-dimensional array  (n*n Matrix) into an one-dimensional array.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>1 2 3
  4 5 6 =&gt; 1 5 9 
  7 8 9</code></pre>
  </div>
  </div>
  </p>
  
  <pre><code>    public int[] getDiagonalFromArray(int[][] two_d_array){
      int[] diagonal_array = new int[two_d_array[0].length];   
      int k=0;
      for (int i = 0; i &lt; two_d_array[0].length; i++) {
          for (int j = 0; j &lt; two_d_array[1].length; j++) {
              for (int l = 0; l &lt; two_d_array[0].length; l++) {  
              diagonal_array[k]=two_d_array[i][j];} //HERE SHOULD BE THE ERROR... HOW DO I CYCLE THROUGH THE 1dim "diagonal_array"? 
              }
          }
      return diagonal_array;
  }
  </code></pre>
  
  <p>This method delivers wrong values.</p>
  
  <p>This method of mine works, but just Prints the diagonale, instead of putting it into an 1dim array.</p>
  
  <pre><code>    public void getDiagonal(int[][] two_d_array){
      //int[] diagonal_array = new int[two_d_array[0].length];        
      for (int i = 0; i &lt; two_d_array[0].length; i++) {
          for (int j = 0; j &lt; two_d_array[1].length; j++) {
  
              if (i==j) System.out.print(two_d_array[i][j]+" ");
          }
      }        
  }
  </code></pre>
  
  <p>Where is the logical difference? I tried the if-clause on the first method, but it raises the "outofbound"-Exception.</p>
  
  <p>Thanks in advance.</p>