title: Rotating a square matrix by 90 degrees
body: <p>Here's my code for rotating a square matrix clockwise by 90 degree in Java.</p>
  
  <pre><code>import java.util.ArrayList;
  
  public class Main {
  
      public static void main(String[] args) {
      // write your code here
          int size = 4;
          int[][] matrix = new int[size][size];
          for (int i = 0; i &lt; size ; i++) {
              for (int j = 0; j &lt; size; j++) {
  
                  matrix[i][j] = (i+(2*j));
              }
          }
          trace(matrix, size);
          rotate(matrix);
          System.out.println("________________________");
          trace(matrix, size);
  
      }
  
      public static void rotate(int[][] matrix){
          int stop = matrix.length -1;
  
          rotate(matrix, stop, 0);
      }
  
      public static void rotate(int[][] matrix, int stop, int start){
  
          if(start - stop == 1) return;
  
          ArrayList&lt;Integer&gt; temp = new ArrayList&lt;&gt;();
  
          //save top row in an array
          for (int i = start; i &lt;= stop ; i++) {
              temp.add(matrix[start][i]);
          }
  
          //left goes to top
          for (int i = start; i &lt;= stop ; i++) {
              matrix[start][i] = matrix[stop - i][start];
          }
  
          //bottom goes to left
          for (int i = start; i &lt;= stop ; i++) {
              matrix[i][start] = matrix[stop][i];
          }
  
          //right goes to bottom
          for (int i = start; i &lt;= stop ; i++) {
              matrix[stop][stop - i] = matrix[i][stop];
          }
  
          //temp array goes to right
          for (int i = 0; i &lt; temp.size() ; i++) {
              matrix[start + i][stop] = temp.get(i);
          }
  
          rotate(matrix, stop-1, start + 1);
      }
  
  
  
      public static void trace(int[][] matrix, int size){
          for (int i = 0; i &lt; size; i++) {
              for (int j = 0; j &lt; size ; j++) {
                  System.out.print(matrix[i][j] + " ");
  
              }
              System.out.println();
          }
      }
  }
  </code></pre>
  
  <p>The original array and the output is shown below,</p>
  
  <pre><code>0 2 4 6 
  1 3 5 7 
  2 4 6 8 
  3 5 7 9 
  ________________________
  3 2 1 0 
  5 4 3 2 
  6 2 5 4 
  3 8 7 6 
  </code></pre>
  
  <p>Clearly the answer is not completely correct. Although it's almost there. I'm a little lost about the bugs, any help appreciated.</p>