Java:创建数组的副本而不将其作为引用
我已经写了一系列矩阵运算,其中我取了一个二维浮点数组,将其视为一个矩阵,并对其执行矩阵运算以获得一个逆矩阵。我的问题是,尽管我与类方法一起使用的数组不是类的一部分,但每次我以数组作为参数运行该方法时,数组本身也会被修改 首先我将描述如何得到矩阵的逆,然后我将显示输出 矩阵求逆的步骤如下:Java:创建数组的副本而不将其作为引用,java,arrays,matrix,copy,pass-by-reference,Java,Arrays,Matrix,Copy,Pass By Reference,我已经写了一系列矩阵运算,其中我取了一个二维浮点数组,将其视为一个矩阵,并对其执行矩阵运算以获得一个逆矩阵。我的问题是,尽管我与类方法一起使用的数组不是类的一部分,但每次我以数组作为参数运行该方法时,数组本身也会被修改 首先我将描述如何得到矩阵的逆,然后我将显示输出 矩阵求逆的步骤如下: 获取辅因子矩阵(即,创建原始矩阵的矩阵子矩阵,然后每隔一个条目求反。如果C=辅因子矩阵,M=子矩阵,i是当前行,j是当前列,则C[i][j]=M[i][j]*(-1)^(i+j) 将辅因子矩阵转换为辅因子矩阵(
public class MatrixOperations {
//Note: this method works fine. There are no problems.
public float determinant(float [][] a)
{
float [][] temp_mat;
float res = 0;
//assuming a square matrix
/*If it's a 2X2, then use the formula for a determinant of
2X2 matrices.*/
if(a.length == 2)
{
return a[0][0]*a[1][1]-a[0][1]*a[1][0];
}
/*Otherwise do the determinant formula recursively until your
determinant is made up of 2X2 matrix determinants and scalar products*/
else
{
temp_mat = new float[a.length-1][a.length-1];
int placej = 0;
int placei = 0;
for(int k = 0; k<a.length;k++)
{
for(int j = 0; j<a.length; j++)
{
for(int i = 1; i < a.length; i++)
{
placei = i-1;
if(j != k)
{
if(j < k)
{
temp_mat[placei][j] = a[i][j];
}
else if(j > k)
{
if (i == 1){
placej = j-1;
}
temp_mat[placei][placej] = a[i][j];
}
}
}
}
res+=a[0][k]*determinant(temp_mat)*(int)Math.pow(-1, k);
}
return res;
}
}
//Note: this method also works fine
//Scalar product method
public float[][] mul(float[][] m, float r)
{
float[][] res = new float[m.length][m.length];
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
res[i][j]= m[i][j]*r;
}
}
return res;
}
//Note: This method also works fine
public float[][] mul(float[][] m,float[][] n)
{
float[][] res = new float[m.length][m.length];
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
for(int k = 0; k < m.length; k++)
{
res[i][j] += m[i][k]*m[k][i];
}
}
}
return res;
}
//The method for creating a matrix of minors
//Here I start having problems
public float[][] minor(float [][] m)
{
float [][] minor_mat = new float [m.length][m.length];
//If the matrix is greater than a 2X2, use this to generate a matrix of minors
if(m.length > 2)
{
float [][] current_minor = new float [m.length-1][m.length-1];
int placei = 0;
int placej = 0;
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
for(int k = 0; k < m.length; k++)
{
for(int l = 0; l < m.length; l++)
{
if(i != k && j != l)
{
if(k<i)
placei = k;
else if(k>i)
placei = k-1;
if(l<j)
placej = l;
else if(l>j)
placej = l-1;
current_minor[placei][placej] = m[k][l];
}
}
}
minor_mat[i][j] = this.determinant(current_minor);
}
}
}
//otherwise use the definition for 2X2 matrix of minors
else
{
//even though minor_mat is using m.clone() rather than m, when I return the result, m has still been modified for some reason.
minor_mat = m.clone()
float temp;
temp = minor_mat[0][0];
minor_mat[0][0] = minor_mat[1][1];
minor_mat[1][1] = temp;
temp = minor_mat[0][1];
minor_mat[0][1] = minor_mat[1][0];
minor_mat[1][0] = temp;
}
return minor_mat;
}
//the same problem occurs here as it did in the minor method
//m appears to get modified even though I only use m.clone()
public float[][] cofactor(float [][] m)
{
float[][] res = m.clone();
res = this.minor(res)
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
res[i][j] = res[i][j]*(int)Math.pow(-1, i + j);
}
}
return res;
}
//The following transpose, adjugate, and inverse methods have the same problem
public float[][] transpose(float[][] m)
{
float[][] res = new float[m.length][m.length];
float temp = 0;
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
temp = m[i][j];
res[i][j] = m[j][i];
res[j][i] = temp;
}
}
return res;
}
public float[][] adjugate(float[][] m)
{
float[][] res = this.transpose(this.cofactor(m));
return res;
}
public float[][] inverse(float[][] m)
{
float[][] res = this.mul(this.adjugate(m), (1/this.determinant(m)));
return res;
}
//print out the matrix in square form
public void matrixprint(float [][] m)
{
for(int i = 0; i < m.length; i++)
{
System.out.println("");
for(int j = 0; j < m[i].length; j++){
System.out.print(m[i][j] + " ");
}
}
System.out.println("\n");
}
}
现在您将看到,当我显示输出时,每次我在“矩阵”上使用一个方法,并重新打印“矩阵”,矩阵本身已被修改,即使我的方法只使用“矩阵”的副本,而不使用“矩阵”本身
输出:
Matrix =
2.0 5.0
4.0 3.0
Minor =
3.0 4.0
5.0 2.0
Matrix =
3.0 4.0
5.0 2.0
Cofactor =
3.0 -4.0
-5.0 2.0
Matrix =
3.0 -4.0
-5.0 2.0
Adjugate =
3.0 5.0
4.0 2.0
Matrix =
3.0 4.0
5.0 2.0
Determinant =
-14.0
Matrix =
3.0 4.0
5.0 2.0
Inverse =
-0.21428573 0.35714287
0.2857143 -0.14285715
Matrix =
3.0 -4.0
-5.0 2.0
Identity =
0.1479592 0.1479592
0.12244898 0.12244898
任何关于为什么会发生这种情况的帮助/解释都将不胜感激。这一行是浅层克隆
float[][] res = m.clone();
这会将res
作为数组引用的数组复制到数组中。但没有任何数组res
指向。很可能您想要的是
float[][] res = new float[m.length][];
for (int i = 0; i < m.length; i++)
res[i] = m[i].clone();
float[]res=新的float[m.length][];
对于(int i=0;i
这是因为在矩阵操作
类的方法中传递了矩阵
对象的引用。它不是矩阵
对象的副本
发件人:
引用数据类型参数(如对象)也传递到
这意味着当方法返回时
传入的引用仍然引用与以前相同的对象
二维数组就是数组的数组。
clone()
在阵列上只执行浅层克隆。
因此,您有一个新的克隆外部数组,但它引用了相同的条目(内部数组)。
克隆外部阵列后,迭代外部阵列并克隆所有内部阵列以获得深度克隆。如果您使用Java 8,当然也可以使用streams和Lambda创建深度克隆。
float[][] res = new float[m.length][];
for (int i = 0; i < m.length; i++)
res[i] = m[i].clone();