title: Swapping Rows and Columns of 2D Array
body: <p>I need to swap rows and columns of a 2D array using Java. I have an <code>ArrayList</code> that tells me where a certain row and column needs to go. For example:</p>
  
  <pre><code>ArrayList&lt;Integer&gt; = [2, 0, 1, 3]
                        0  1  2  3 (indexes for illustration)
  </code></pre>
  
  <p>The above means that row 0 and column 0 need to become row 2 and column 2, row 1 and column 1 need to become row 0 and column 0, and so on.</p>
  
  <p>For example:</p>
  
  <pre><code>int[][] example = {
      {0, 3, 3, 4},
      {0, 1, 0, 0},
      {0, 0, 1, 0},
      {8, 1, 1, 0}
  };
  </code></pre>
  
  <p>Let's say we swap rows first, so the "intermediate" form would be:</p>
  
  <pre><code>// From the list, change rows as follows: 
  // 0-&gt;2, 1-&gt;0, 1-&gt;2, 3-&gt;3
  int[][] example = {
      {0, 1, 0, 0},
      {0, 0, 1, 0},
      {0, 3, 3, 4},
      {8, 1, 1, 0}
  };
  </code></pre>
  
  <p>Finally, swapping columns, we get the desired output:</p>
  
  <pre><code>// From the list, change columns as follows: 
  // 0-&gt;2, 1-&gt;0, 1-&gt;2, 3-&gt;3
  int[][] example = {
      {1, 0, 0, 0},
      {0, 1, 0, 0},
      {3, 3, 0, 4},
      {1, 1, 8, 0}
  };
  </code></pre>
  
  <p>Note that the swapping may be in place or in a new matrix, does not matter.
  I'm stuck in the part where I need to swap the columns, I'm not too sure how to proceed here.
  This is what I've tried so far:</p>
  
  <pre><code>public static int[][] makeStandardForm(int[][] m){
      //generate list of index swaps
      List&lt;Integer&gt; l = new ArrayList&lt;Integer&gt;(orderIndexes(m));
      int[][] m1 = new int[m.length][m.length];
  
      //Swap rows, working fine
      for(int i=0; i &lt; m.length; i++){
          m1[i] = m[(int)l.get(i)];
      }
  
      //Swap columns, stuck here?
      for(int i=0; i &lt; m.length; i++){
          //don't know how to swap columns
       }
       return m1;
   }
  </code></pre>