title: How to flip rows in a 1D short/int array representing a 2D array in Java
body: <p>I need to flip a 1-D 64-element array of shorts (I can switch to ints if it's easier, but I assume the same processes will work for either) on it's head in Java. I represent it here as a square table for ease of understanding, since the actual problem is on a chessboard.</p>
  
  <p>For example:</p>
  
  <pre><code>short[] example = new short[]
  {
      1, 2, 3,
      4, 5, 6,
      7, 8, 9
  };
  </code></pre>
  
  <p>would become:</p>
  
  <pre><code>7 8 9
  4 5 6
  1 2 3
  </code></pre>
  
  <p>Please note that this is <strong>NOT</strong> the same as reversing the array (every answerer to similar questions I have found has made this mistake, hence my having to ask!). Reversing the array would give:</p>
  
  <pre><code>9 8 7
  6 5 4
  3 2 1
  </code></pre>
  
  <p>Apologies if I've missed any important info, any help is appreciated!</p>
  
  <p>EDIT: The array is 1D and contains 64 elements, so short[64], and the reversed array is separate to the original. As far as what I've tried, I'm just struggling to wrap my head around it. I know how to reverse the array, but that's not what I'm after, and I had originally tried to reverse the index using:</p>
  
  <pre><code>byte index = (byte)(((byte)(position + 56)) - (byte)((byte)(position / 8) * 16));
  </code></pre>
  
  <p>Which is a code snippet I found on <a href="http://www.chessbin.com/post/Piece-Square-Table.aspx" rel="nofollow">Chessbin</a>, but this returns incorrect values and gives IndexOutOfBounds errors. In hindsight it's not clear to me if that code is meant to flip the index or reverse it. Since maths is not my strong suit, I tried to work around it with separate arrays.</p>