2013-04-15 4 views
0

우선 순위 대기열 작업에 익숙하지 않으며이 코드의 형식이 잘못되었습니다. 도시에 대한 직선 거리가되도록 우선 순위를 지정하고 싶지만 정보를 올바르게 큐에 저장하십시오. API를 보면서 나는

지정된 콤퍼레이터에 따라 요소를 순서 지정된 초기 용량을 사용해, PriorityQueue를 작성합니다 (파라미터 : initialCapacity, 비교기 비교기를 int로)를 비교우선 순위 대기열을 특정 유형에 적용 할 수 없습니다.

공공 PriorityQueue 인으로 SLD를 설정해야합니다.

그러나 이것은 나에게 명확하지 않습니다.

public static void GreedySearchMap(map Romania) { 
    boolean done = false; 

    city current; 

    int numsteps = 10; 
    int cursteps; 
    int choice; 
    int numconnections; 
    int totaldist; 

    cursteps = 0; 
    current = Romania.Arad; 
    totaldist = 0; 

    /*create queue*/ 
    PriorityQueue<city> q = new PriorityQueue<city>(city city,int SLD);   
    q.offer(current); 
    current.visited = true; 

    while (!done) { 
     System.out.printf("Step %d, In %s, Distance\t%d\n", cursteps, 
       current.getname(), totaldist); 

     if (current.getname() == "Bucharest") 
      done = true; 
     else { 

      current = q.poll(); 
      cursteps++; 
      numconnections = current.getconnections(); 

      for (int i = 0; i < numconnections; i++) { 
       choice = i; 
       if (current.getcity(choice).visited == false) { 
        //totaldist += current.getdist(choice); 
        q.offer(current.getcity(choice), current.getSLD()); 
        current.visited = true; 
       } 
      } 
     } 
    } 

    System.out.printf("-----------------------\n"); 
} 

내 오류는 다음과 같습니다

P:\csci395\hw4>javac GS.java 
GS.java:85: error: method offer in class PriorityQueue<E> cannot be applied to g 
iven types; 
                  q.offer(current. 
getcity(choice), current.getSLD()); 
                  ^
    required: city 
    found: city,int 
    reason: actual and formal argument lists differ in length 
    where E is a type-variable: 
    E extends Object declared in class PriorityQueue 
1 error 
+0

'if (current.getname() == "Bucharest")'문자열을 '=='와 비교하지 마십시오. 대신에 .equals()를 사용하십시오. 가장 좋은 방법은''stringLiteral ".equals (stringVariable)'과 같이 사용하는 것입니다. 왜냐하면 NullPointerException을 피할 수 있기 때문입니다. – RaptorDotCpp

+0

'city city, int SLD'는 매개 변수가 선언되는 방식이며,'new PriorityQueue (city city, int SLD)'는 생성자 호출 (정의가 아님)입니다. '새로운 PriorityQueue (someInteger, anInstanceOfCaparator)'여야합니다. 인수 someInteger, anInstanceOfCaparator는 생성자 서명이 요구하는 유형이어야합니다. –

답변

0

SLD는 Comparator 아니라, 그냥 일이 비교되는 것입니다. 당신은 실제 비교를 수행하는 클래스를 작성해야하고, 그 제출 : java.util.Comparator 읽기 최대

new Comparator<city>() { 
    @Override 
    public int compare(city city1, city city2) { 
     return city1.getSLD() - city2.getSLD(); 
    } 
}; 

더 친해지고.

  • 편집 -

이 있지만, 단 하나의 오류입니다. 컴파일러의 출력에서 ​​설명한대로 컴파일에서 얻은 오류는 PriorityQueue의 offer() 메서드에 잘못된 인수를 사용하고 있기 때문에 발생합니다. city 만 전달하면됩니다. SLD는 Comparator 인스턴스의 코드에 의해 처리됩니다.

0

먼저 클래스에 대문자를 입력하십시오. 'city city'는 컴파일러 오류를 생성 할 수 있습니다. 두 번째로, 오류 메시지가 무엇이 잘못되었는지를 알려줍니다.

PriorityQueue에 비교자를 지정해야합니다. int는 클래스 객체조차 아니다. 원시 타입이다. 자세한 정보는 Java Comparators (java.lang.Comparator)를 참조하십시오.

관련 문제