1

This is my class with my methods

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;
    }
  }

}

This is my test class

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"); }

    }
  }

This is my output

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

Why is MatrixA being modified after I perform a swap on One?

*Note This prints a row downwards because I didn't want to do all the print formatting.

1 Answer 1

6

Your Matrix constructor just copies the array reference :

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

Hence both matrixA and the array contained in the One object are the same array.

If you don't want your Matrix instances to modify the original input array, you must create copy the arrays :

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I had a feeling it had something to do with the constructor, but I forgot I had to copy the data over rather than just pointing to the same array in memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.