Int不能在Equals方法中取消引用

时间:2014-03-05 23:01:06

标签: java equals

我在Position类中有一个equals方法。该类包含两个实例变量row和col,它们表示网格中的位置。我需要能够使用equals方法检查两个不同的位置是否相等。但是,使用我当前的equals方法,我在编译时得到“Int not be dereferenced”错误。我找不到如何解决这个问题,并且教师指定应该没有理由覆盖哈希码。我的平等方法如下,任何帮助都表示赞赏。

@Override
    public boolean equals(Object other) {
        if (null == other) return false;
        if (this == other) return true;
        if (!(other instanceof Position)) return false;
        Position that = (Position) other;
        return row.equals(that.getRow()) && col.equals(getCol());
    }

编辑: row和col的类型为int。

1 个答案:

答案 0 :(得分:2)

如果row和/或columnint,则它根本没有任何方法,因为它是基本类型。将其更改为Integer或改为使用==

相关问题