有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java对相邻匹配项进行点检查,然后让这些相邻匹配项检查匹配项并将所有项添加到哈希集

我试图将一个点的相邻匹配添加到哈希集中,然后检查这些相邻点的相邻相等点,并将它们添加到同一哈希集中。我在添加相邻点和检查相邻点时遇到问题。这和宝石火柴的制作方法很相似

public class matchFinder {

    private HashSet<Point> adjacentAndMatching(Point p, ArrayList<ArrayList<String>> board) {
        HashSet<Point> result = new HashSet<Point>();
        if (p != null) {
            Point check;
            check = new Point(p.x-1,p.y); if (inBounds(check, board) && matches(p,check,board)) { result.add(check); }
            check = new Point(p.x+1,p.y); if (inBounds(check, board) && matches(p,check,board)) { result.add(check); }
            check = new Point(p.x,p.y-1); if (inBounds(check, board) && matches(p,check,board)) { result.add(check); }
            check = new Point(p.x,p.y+1); if (inBounds(check, board) && matches(p,check,board)) { result.add(check); }
        }
        return result;
    }

    private boolean matches(Point p, Point q, ArrayList<ArrayList<String>> _board) {
        return _board.get(p.x).get(p.y).equals(_board.get(q.x).get(q.y));
    }

    private boolean inBounds(Point p, ArrayList<ArrayList<String>> _board) {
        return p.x >=0 && p.x < _board.size() && p.y >= 0 && p.y < _board.get(0).size();
    }

}

共 (1) 个答案

  1. # 1 楼答案

    您是否重写了Point类中的hashcode方法?对于自定义类,您需要这样做,以指定在向哈希集添加元素时如何进行哈希