我想为equals()
我拥有的一个类实现一个自定义方法,Board
. 该方法比较每个板的数组,定义为private int[] board
,如果数组相等则返回 true,否则返回 false。我知道在测试相等性时有一些“陷阱”,所以我想知道以下代码是否是最佳的并且足以真正测试相等性:
public boolean equals(Object y) {
if (this.getClass() != y.getClass()) return false; //must be same class -- duh
Board that = (Board) y; //y cast as Board
int[] thisBoardCopy = this.getBoard(); //copy of current board
int[] thatBoardCopy = that.getBoard(); //copy of y's board
return Arrays.equals(thisBoardCopy, thatBoardCopy);
}