我是Java的新手, 目前,我正在练习一些Java代码。所以我想尝试自己构建一个Matrix类。但是,我引用了jama(http://math.nist.gov/javanumerics/jama/doc/Jama/Matrix.html)的代码。 但我觉得很奇怪。这是Matrix类的结构,Jama在后面部分定义。
有人可以帮我解释为什么transpose()返回X(在我看来,C数组是X的变换元素,X的元素是相同的顺序。但是为什么jama返回X,以及C数组的作用是什么在这个程序?)。 非常感谢你。
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;
}
}
答案 0 :(得分:1)
X
和C
之间存在关联。当您致电getArray()
时,它会返回A
本身,而不是A
的副本。
因此,在transpose()
方法的上下文中,C
与X.A
相同。
您可以在java
中了解引用变量的行为答案 1 :(得分:0)
C
只是一个变量,它引用了与X.A
相同的二维数组。