문제

I have an equals method in a Position class. The class contains two instance variables, row and col, that represent the location in a grid. I need to be able to check if two different positions are equal, using an equals method. However, with my current equals method I get an "Int cannot be dereferenced" error upon compiling. I cannot find how to fix this, and the instructor has specified that there should be no reason to overwrite the hashcode. My equals method is below, any help is appreciated.

@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());
    }

EDIT: row and col are of type int.

도움이 되었습니까?

해결책

If row and/or column is int, then it doesn't have any method at all since it is a primitive type. Change it to Integer or use == instead.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top