@w1024020103
2017-01-25T12:02:52.000000Z
字数 879
阅读 668
Java
equals
(bjsxt)
When doing coursera Algorithem week4 assignment and determining whether two 3-3 boards are equal, I wrote codes like this:
public boolean equals(Object y){ // does this board equal y?
if (y == this) return true; //check out Transaction.java and imitate
if (y == null) return false;
if (y.getClass() != this.getClass()) return false;
Board that = (Board) y;
if(this.dimension() != that.dimension()) {
return false;
}
//return (this.board == that.board) && (this.n == that.n); this could be reference equal
for(int i = 0; i<n; i++){
for(int j = 0; j< n; j++) {
if (this.board[i][j] != that.board[i][j]) {
return false;
}
}
}
return true;
}
This brought me to take a deeper look at the equals method.