Это мой класс с моими методамиПочему мой код изменяет неправильный массив?
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
Почему матрица А модифицируется после того, как я выполнить обмен на Один?
* Примечание. Это печатает строку вниз, потому что я не хочу делать все форматирование печати.
Спасибо, у меня было такое ощущение, что что-то делать с конструктором, но я забыл, что мне пришлось копировать данные, а не просто указывать на один и тот же массив в памяти. –