title: Traverse an array diagonally
body: <p>I have a large array of arbitrary size. It's a square array. I'm trying to grasp how to traverse it diagonally like a <code>/</code> rather than a <code>\</code> (what I already know how to do). I have the following code thus far:</p>
  
  <pre><code>char[][] array = new char[500][500];
  //array full of random letters
  String arrayLine = "";
  for (int y = 0; y &lt; array.length; y++) {
      for (int x = 0; x &lt; array.length; x++) {
          for (???) {
              arrayLine = arrayLine + array[???][???];
          }
      }
      System.out.println(arrayLine);
  }
  </code></pre>
  
  <p>I have three loops, because this is how I did the other diagonal:</p>
  
  <pre><code>for (int y = 0; y &lt; array.length; y++) {
      for (int x = 0; x &lt; array.length; x++) {
          for (int z = 0; z &lt; array.length-y-x; z++) {
              arrayLine = arrayLine + array[y+z][x+z];
          }
      }
      System.out.println(arrayLine);
  }
  </code></pre>
  
  <p>In my attempts, I keep going outside the boundaries and get an ElementOutOfBounds exception. Say the array is as below (a 3x3 instead of 500x500):</p>
  
  <pre><code>A B C
  D E F
  G H I
  </code></pre>
  
  <p>I want to print out the following as strings:</p>
  
  <pre><code>A
  BD
  CEG
  FH
  I
  </code></pre>
  
  <p>A previous SO question had a similar problem with integer arrays, and the solution is based off of the sum of array elements. But I'm working with chars, so I can't think of a methodology to get it.</p>