title: Matrix Transpose in Java
body: <p>I have a method transpose in class Matice. When i apply this method on my m2.matice i can see during debugging that my matrix is being transposed, but when i print m2 i get the same matrix as before. </p>
  
  <pre><code>    public class Main {
      public static void main(String[] args) {
          Matice m1 = new Matice(5);
          m1.matrixFill(0, 5);
          m1.matrixPrint();
  
          Matice m2 = new Matice(3);
          m2.matrixFill(-5, 10);
          m2.matrixPrint();
  
          m2.transpose();
          m2.matrixPrint();
      }
  }
  
  package matice;
  /**
   *
   * @author Adam Rychter
   */
  public class Matice {
  
      int[][] matice;
      private int n;
  
      public Matice(int n) {
          this.n = n;
          if(n &gt; 0) {
              matice = new int[n][n];
          }
      }
  
      public void matrixPrint(){
  
          for (int i = 0; i &lt; n; i++) {
              for (int j = 0; j &lt; n; j++) {
                  System.out.format("%5d", matice[i][j]);                
              }
              System.out.println("");
          }
          System.out.println("");
      }
  
      public void matrixFill(int a, int b){
          for (int i = 0; i &lt; n; i++) {
              for (int j = 0; j &lt; n; j++) {
                  matice[i][j] = (int) (Math.random() * (a + b + 1) - a);                
              }
          }
      }
  
      public void transpose(){
          int tmp;
          for (int i = 0; i &lt; n; i++) {
              for (int j = 0; j &lt; n; j++) {
                  tmp = matice[i][j];
                  matice[i][j] = matice[j][i];
                  matice[j][i] = tmp;                
              }            
          }
      }
  }
  </code></pre>