2016-12-03 12 views
0

ポイントに隣接する一致をHashSetに追加しようとしています。それらを同じHashSetに追加します。隣接する点を追加したり、隣接する点をチェックするのに問題があります。それは、宝石飾りが行われる方法と似ています。隣接する一致の点検を行い、隣接する一致をチェックして一致をチェックし、すべてをHashSetに追加します

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

} 

答えて

0

Pointクラスのhashcodeメソッドをオーバーライドしましたか?ハッシュセットに要素を追加するときにハッシュがどのように行われるかを指定するために、カスタムクラスに対してこれを行う必要があります。

関連する問題