-1

I am a newbie to Java, Currently, I am practicing some codes of Java. So I am trying to build a Matrix class myself. However, I refers to the code from jama (http://math.nist.gov/javanumerics/jama/doc/Jama/Matrix.html). But I feel it quite strange. Here is the structure of Matrix class, Jama defined in the later part.

Can somebody help me explain why transpose() return X (in my thinking, the C array is stransposed elements of X,elements of X is same order. But why jama return X, and what is the role of C-array in this program?). Thank you so much.

public class Matrix
{

    private double[][] A;// 2-D array to hold matrix element
    private m,n ; // number of column and row.
    // Some constructors but I would like to omit

    //public methods:
     // I don't understand this:

   public Matrix transpose () {
      Matrix X = new Matrix(n,m);
      double[][] C = X.getArray();
      for (int i = 0; i < m; i++) {
         for (int j = 0; j < n; j++) {
            C[j][i] = A[i][j];
         }
      }
      return X; // While it returns X? seem that X does not transpose but C.
                // it seems there is no connection between X and C. what  is the role of C here?
   }


   public double[][] getArray () {
      return A;
   }       

}
  • Possible duplicate of [transpose double\[\]\[\] matrix with a java function?](http://stackoverflow.com/questions/15449711/transpose-double-matrix-with-a-java-function) – Debosmit Ray Mar 07 '16 at 05:11

2 Answers2

1

There is a connection between X and C. When you call getArray() it return A itself, not a copy of A.

So in the context of transpose() method, C is the same as X.A.

You can read about the behaviour of reference variables in java

Maljam
  • 6,244
  • 3
  • 17
  • 30
  • Yes, I love the word ("There is a connection between X and C. When you call getArray() it return A itself, not a copy of A. "). How can I click the button accept the answer? I could not find that button. thank you so much you guys. – Hien Nguyen Mar 07 '16 at 05:20
0

C is just a variable with a reference to the same 2D array as X.A.

Andreas
  • 154,647
  • 11
  • 152
  • 247