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

时间:2016-09-01 07:39:37

标签: java arrays access-control

这是我的方法

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

为什么在执行一次交换后,MatrixA会被修改?

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

1 个答案:

答案 0 :(得分:6)

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

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

因此matrixAOne对象中包含的数组都是相同的数组。

如果您不希望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);
}