gpt4 book ai didi

java - 为什么我的代码修改了错误的数组?

转载 作者:行者123 更新时间:2023-11-29 06:53:42 25 4
gpt4 key购买 nike

这是我的类(class),我的方法

public class Matrix  {
//this is the stored matrix given from another class
private double[][] stored;

//constructor
public Matrix(double[][] input){
stored = input;
}

public double[] getRow(int rownum){
return stored[rownum];
}

//change current matrix by swapping rows rowA and rowB
public void swap(int rowA, int rowB){
double[] temp = stored[rowA];
stored[rowA] = stored[rowB];
stored[rowB] = temp;
}

// looking only at column column_num in matrix,
// return index of which row has largest value
public int maxRow(int column_num){
double largest = 0.0; //current largest value
int index = 0; //index of row with largest value
for(int i = 0; i < stored.length; i++ ){
if(stored[i][column_num] > largest){
largest = stored[i][column_num];
index = i;
}
}
return index;
}

//method to iterate through a row and scale by factor
public void scaleRow(int rownum, double factor){
for(int i = 0; i < stored[rownum].length; i++){
double cValue = stored[rownum][i];
stored[rownum][i] = cValue*factor;
}
}

}

这是我的测试课

import java.util.Arrays;
public class MatrixTest {
public static void main(String[] args) {
/* test cases for Matrix class methods */
double[][] matrixA = {{1,2,3,4},{2,4,6,8},{9,10,11,12}};
double[][] matrixB = {{0.92,-900,44},{1201,18.264,-21.0},{0,0,0}};
double[][] matrixC = {{1.5E-12,-6.034E2},{41.8,-125E-3}};
Matrix One = new Matrix(matrixA);
Matrix Two = new Matrix(matrixB);
Matrix Three = new Matrix(matrixC);

/* test whether or not maxRow works properly */
if (One.maxRow(0) == 2) {
System.out.println("Passed maxRow test for matrix One"); }
else { System.out.println("Failed maxRow test for matrix One"); }
if (Two.maxRow(1) == 1) {
System.out.println("Passed maxRow test for matrix Two"); }
else { System.out.println("Failed maxRow test for matrix Two"); }

/* test whether or not scaleRow works properly */
One.scaleRow(0,2.0); // scale row 0 by 2.0
if (Arrays.equals(One.getRow(0),matrixA[1])) {
System.out.println("Passed scaleRow test for matrix One"); }
else { System.out.println("Failed scaleRow test for matrix One"); }
Two.scaleRow(2,12.608); // scale row 2 by 12.608
if (Arrays.equals(Two.getRow(2),matrixB[2])) {
System.out.println("Passed scaleRow test for matrix Two"); }
else { System.out.println("Failed scaleRow test for matrix Two"); }
One.scaleRow(0,0.5); // scale row 0 by 0.5
if (Arrays.equals(One.getRow(0),matrixA[0])) {
System.out.println("Passed scaleRow test for matrix Three"); }
else { System.out.println("Failed scaleRow test for matrix Three"); }

for(int i = 0; i < matrixA[2].length; i++){
System.out.println(matrixA[2][i]);
}
System.out.println("This prints row 2 of matrixA before the swap");

/* test whether or not swap method works properly */
One.swap(2,0); // swap contents of Row 2 with Row 0
if (Arrays.equals(One.getRow(0),matrixA[2]) &&
Arrays.equals(One.getRow(2),matrixA[0])) {
System.out.println("Passed swap test for matrix One"); }
else {
double[] row0 = One.getRow(0);
for(int i = 0; i < row0.length; i++){
System.out.println(row0[i]);
}
System.out.println("This prints row 0 of One after the swap");
for(int i = 0; i < row0.length; i++){
System.out.println(matrixA[2][i]);
}
System.out.println("This prints row 2 of matrixA after the swap");
System.out.println("Failed swap test for matrix One");
}
Two.swap(0,1); Two.swap(1,2); Two.swap(1,0); // fancy swap sequence
if (Arrays.equals(Two.getRow(0),matrixB[2]) &&
Arrays.equals(Two.getRow(2),matrixB[0])) {
System.out.println("Passed swap test for matrix Two"); }
else { System.out.println("Failed swap test for matrix Two"); }

}
}

这是我的输出

Passed maxRow test for matrix One
Passed maxRow test for matrix Two
Passed scaleRow test for matrix One
Passed scaleRow test for matrix Two
Passed scaleRow test for matrix Three
9.0
10.0
11.0
12.0
This prints row 2 of matrixA before the swap
9.0
10.0
11.0
12.0
This prints row 0 of One after the swap
1.0
2.0
3.0
4.0
This prints row 2 of matrixA after the swap
Failed swap test for matrix One
Failed swap test for matrix Two

为什么在我对 One 执行交换后 MatrixA 被修改了?

*注意这会向下打印一行,因为我不想做所有的打印格式。

最佳答案

您的 Matrix 构造函数只是复制数组引用:

public Matrix(double[][] input){
stored = input;
}

因此 matrixA 和包含在 One 对象中的数组是同一个数组。

如果您不希望您的 Matrix 实例修改原始输入数组,您必须创建数组副本:

public Matrix(double[][] input){
// stored = Arrays.copyOf(input,input.length); // this would only work for 1D arrays
stored = new double[input.length][];
for (int i = 0; i < input.length; i++)
stored[i] = Arrays.copyOf(input[i],input[i].length);
}

关于java - 为什么我的代码修改了错误的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39265494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com