우선 순위 대기열 작업에 익숙하지 않으며이 코드의 형식이 잘못되었습니다. 도시에 대한 직선 거리가되도록 우선 순위를 지정하고 싶지만 정보를 올바르게 큐에 저장하십시오. 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
'if (current.getname() == "Bucharest")'문자열을 '=='와 비교하지 마십시오. 대신에 .equals()를 사용하십시오. 가장 좋은 방법은''stringLiteral ".equals (stringVariable)'과 같이 사용하는 것입니다. 왜냐하면 NullPointerException을 피할 수 있기 때문입니다. – RaptorDotCpp
'city city, int SLD'는 매개 변수가 선언되는 방식이며,'new PriorityQueue (city city, int SLD)'는 생성자 호출 (정의가 아님)입니다. '새로운 PriorityQueue (someInteger, anInstanceOfCaparator)'여야합니다. 인수 someInteger, anInstanceOfCaparator는 생성자 서명이 요구하는 유형이어야합니다. –