这是我当前的 equals 实现,它比较两个矩阵是否相同。问题是当我运行测试时它说它们是不同的。
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj == null) return false;
if (!(obj instanceof Matrix)) return false;
Matrix matrix = (Matrix) obj;
int counter = 0;
if(this.rowIndex == matrix.rowIndex && this.columnIndex == matrix.columnIndex){
for(int i=0; i<this.rowIndex; i++)
for(int j=0; j<this.columnIndex; j++)
if(this.matrix[i][j] == matrix.matrix[i][j]);
counter++;
}
return counter == (matrix.rowIndex * matrix.columnIndex);
}
@Test
void changeValorTest(){
Matrix matrix1 = new Matrix(3, 3, true);
matrix1.changeValor(1,1,5);
Integer[][] matrix2Real = {{0, 0, 0}, {0, 5, 0}, {0, 0, 0}};
Matrix matrix2 = new Matrix(matrix2Real, true);
assertEquals(matrix1.matrix, matrix2.matrix);
assertThrows(IllegalArgumentException.class, () -> matrix1.changeValor(0,-10, 7));
}
和输出:
org.opentest4j.AssertionFailedError: expected: [[Ljava.lang.Integer;@305fd85d<[[0, 0, 0], [0, 5, 0], [0, 0, 0]]> but was: [[Ljava.lang.Integer;@7a1ebcd8<[[0, 0, 0], [0, 5, 0], [0, 0, 0]]>
Expected :[[Ljava.lang.Integer;@305fd85d
Actual :[[Ljava.lang.Integer;@7a1ebcd8
我在 Matrix 类中有 equals。是因为它的实施还是我做错了什么?