2012-09-23 3 views
3
public class state implements Comparator<state>{ 
     Point a; 
     Point b; 
     private int path_cost=0; 
     ... 
} 

    class Point { 
     int x; 
     int y; 
     ... 
    } 

를 포함 : 나는 openNode 또는 closed ANY의 Point acurrNodePoint a 동일한 경우 확인해야내가이 이상에 대한 기능 LinkedList의

PriorityQueue<state> openNode= new PriorityQueue<state>(); 
LinkedList<state> closed =new LinkedList<state>(); 
state currNode; 

.

내가 전체 개체와 일치해야한다면 을 사용할 수 있지만 여기에서는 상태 클래스의 하나의 variabale (Point a) 만 신경 씁니다. 나는이 메소드가 PriorityQueue와 LinkedList의 모든 노드를 검사하기를 원한다.

추가 : 나는 priorityQueue 및 LinkedList에서 Iterator를 사용하려고합니다. 하지만 Iterator를 사용하여 Point a의 값을 읽는 방법을 모르겠습니다.

+0

하나를 써야합니다. 질문 있습니까? –

+0

위의 방법을 생각할 수 없습니다! – change

답변

2

편집 : 나는 약간 오해했을 것 같습니다. 내가 생각했던 것보다 더 간단하다.

// I've assumed more conventional names 
Point currPoint = currNode.getPointA(); 
for (State openNode : openNodes) { 
    if (openNode.getPointA().equals(currPoint)) { 
     return true; 
    } 
} 

for (State closedNode : closedNodes) { 
    if (closedNode.getPointA().equals(currPoint)) { 
     return true; 
    } 
} 
// No matching points 
return false; 

당신은 가능성이 약간 간단하게 구아바의 Iterables.concat() 방법을 사용할 수 있습니다

:

for (State node : Iterables.concat(closedNodes, openNodes)) { 
    if (node.getPointA().equals(currPoint)) { 
     return true; 
    } 
} 
return false; 

당신이 노드가 동일한 점 A를 가지고 을 알고 싶다면, 단지로 변경

for (State node : Iterables.concat(closedNodes, openNodes)) { if (node.getPointA().equals(currPoint)) { return node; } } return null; 

그런 노드는 하나만 있습니다. 물론이 노드는 여러 경기가있을 수 있습니다.

+0

currNode의'Point a'가 어느 정도 일치하면'path_cost' 변수를 사용해야합니다. 이 경우 나는 그 연결을 잃을 것이다. 내 priorityQueue 및 LinkedList에서 Iterator를 사용하려고 생각했습니다. 하지만 Iterator를 사용하여 Point a의 값을 읽는 방법을 모르겠습니다. – change

+0

@parin : 질문을 바꾸고 있습니다. 원래 그 노드가 있는지 여부 만 알고 싶었습니다. 내 대답을 편집했습니다. –

0

state 클래스의 경우 Point a에 equals 메소드를 제공하거나 간단한 반복을 사용하고 비교를 위해 List를 반복해야합니다. contains 방법도 동일합니다.

다른 방법을 사용하면 시간이 많이 소요됩니다.

아주 이상한 방법은 Comparator to check equality

class PointAComparator implements Comparator<State> 

{ 
    Point p = null; 
    public PointAComparator(Point a) { 
     p = a; 
    } 
    @Override 
    public int compare(State o1, State o2) { 
     return (p.x == o1.a.x && p.y == o1.a.y) ? 1 
       : (p.x == o2.a.x && p.y == o2.a.y) ? 1 : -1; 
    } 
} 

는 위의 방법이 다른 동일한 1을 반환 비교 -1 그래서 당신은 동일한 시작에 요소를 가질 것이다 각 목록을 정렬 할 때 사용할 수 있습니다. 첫 번째 요소를 확인할 수 있습니다.

0

나는 두 가지 개체에 대해 equals 함수에서 재정의하는 메서드를 사용하여 내 결과를 달성했습니다.

 class Point { 
      int x; 
      int y; 
      ... 

    @Override 
    public boolean equals(Object other){ 
     if (other == null) return false; 
     if (other == this) return true; 
     if (!(other instanceof Point))return false; 
     Point otherPoint = (Point)other; 
     return (this.x==otherPoint.getX() && this.y==otherPoint.getY())? true : false; 
    } 

     } 



public class state implements Comparator<state>{ 
      Point a; 
      Point b; 
      private int path_cost=0; 
      ... 
    @Override 
    public boolean equals(Object other){ 
     if (other == null) return false; 
     if (other == this) return true; 
     if (!(other instanceof state))return false; 
     state otherState = (state)other; 
     return ((this.a).equals(otherState.a))? true : false; 
    } 
    }