Java Matrix Transpose strangeness going on

Posted by user1459976 on Stack Overflow See other posts from Stack Overflow or by user1459976
Published on 2012-10-18T04:47:40Z Indexed on 2012/10/18 5:02 UTC
Read the original article Hit count: 161

Filed under:
|
|
|
|

ok so im making my own Matrix class. and i have a transpose method that transposes a matrix. this is the block in the main method

    Matrix m1 = new Matrix(4,2);
    m1.fillMatrix(1,2,3,4,5,6,7,8);
    System.out.println("before " + m1.toString());
    m1.transpose();
    System.out.println("after " + m1.toString());

this is where it gets messed up, at m1.transpose(); in the transpose() method

public Matrix transpose() {
    if(isMatrix2) {
        Matrix tempMatrix = new Matrix(row, col); // matrix2 contents are emptied once this line is executed
        for(int i=0; i < row; i++) {
            for(int j=0; j < col; j++)
                tempMatrix.matrix2[i][j] = matrix2[i][j];
        }

so for some reason, the tempMatrix.matrix2 has the same id as this.matrix2. so when the codes executes

 Matrix tempMatrix = new Matrix(row,col);

then the contents of this.matrix2 is emptied. anyone know what might be going on here?

© Stack Overflow or respective owner

Related posts about java

Related posts about class