Exception in thread "main" java.lang.ClassCastException: javafx.util.Pair cannot be cast to java.lang.Comparable

huangapple go评论71阅读模式
英文:

Exception in thread "main" java.lang.ClassCastException: javafx.util.Pair cannot be cast to java.lang.Comparable

问题

以下是您提供的代码的翻译部分:

还有这个问题已经被问了很多次根据我的解决方案我无法解决这个问题我正在编写用于加权图的Dijkstra算法我使用了ArrayList<ArrayList<Pair<Integer, Integer>>>来存储我的权重和边缘在实现Dijkstra函数时我使用了MinHeap它是Java中Pair<Integer, Integer>类型的PriorityQueue在将对添加到堆时我遇到了这个错误

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import javafx.util.Pair;

public class Solution {

    public static void dijkstra(ArrayList<ArrayList<Pair<Integer, Integer>>> graph, int V, int E, int S) {
        int dist[] = new int[V + 1];
        Arrays.fill(dist, Integer.MAX_VALUE);
        dist[S] = 0;

        PriorityQueue<Pair<Integer, Integer>> heap = new PriorityQueue<>();

        heap.add(new Pair<Integer, Integer>(0, S));

        while (heap.size() != 0) {
            Pair<Integer, Integer> curr = heap.poll();
            int w = curr.getKey(), x = curr.getValue();
            System.out.println(x);
            if (w <= dist[x]) {
                for (Pair<Integer, Integer> temp : graph.get(x)) {
                    int x2 = temp.getKey(), w2 = temp.getValue();
                    if (w + w2 < dist[x2]) {
                        dist[x2] = w + w2;

                        // 在下面的行中遇到错误
                        heap.add(new Pair<Integer, Integer>(w + w2, x2));
                    }
                }
            }
        }
        for (int i = 1; i <= V; i++) {
            if (i == S)
                continue;
            if (dist[i] == Integer.MAX_VALUE)
                System.out.print("-1 ");
            else
                System.out.print(dist[i] + " ");
        }
    }

    // 向图中添加边
    public static void addEdge(ArrayList<ArrayList<Pair<Integer, Integer>>> graph, int u, int v, int w) {
        graph.get(u).add(new Pair<Integer, Integer>(v, w));
        graph.get(v).add(new Pair<Integer, Integer>(u, w));
    }

    // 主函数
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        while (t-- != 0) {
            int n = in.nextInt();
            int m = in.nextInt();
            ArrayList<ArrayList<Pair<Integer, Integer>>> graph = new ArrayList<>();
            for (int i = 0; i <= n; i++) {
                graph.add(new ArrayList<Pair<Integer, Integer>>());
            }

            for (int i = 0; i < m; i++) {
                addEdge(graph, in.nextInt(), in.nextInt(), in.nextInt());
            }
            int s = in.nextInt();
            dijkstra(graph, n, m, s);
            System.out.println();
        }
    }
}

希望这有助于您理解代码的翻译部分。如果您有任何其他问题,请随时提问。

英文:

Also, this question had asked many times, I'm unable to figure that out according to my solution. I'm writing the Dijkstra algorithm for the weighted graph. I used ArrayList<ArrayList<Pair<Integer, Integer>>> for storing my weights and edges. While the implementation of the Dijkstra function, I used MinHeap which is PriorityQueue in java of type Pair<Integer, Integer>. At the time of adding pairs to the heap, I getting this error.

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import javafx.util.Pair; 
public class Solution{
public static void dijkstra(ArrayList&lt;ArrayList&lt;Pair&lt;Integer, Integer&gt;&gt;&gt; graph, int V, int E, int S) {
int dist[] = new int[V+1];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[S] = 0;
PriorityQueue&lt; Pair&lt;Integer, Integer&gt; &gt; heap = new PriorityQueue&lt;&gt;();
heap.add(new Pair&lt;Integer, Integer&gt;(0, S) );
while(heap.size() != 0) {
Pair&lt;Integer, Integer&gt; curr = heap.poll();
int w = curr.getKey(), x = curr.getValue();
System.out.println(x);
if(w &lt;= dist[x]) {
for(Pair&lt;Integer, Integer&gt; temp : graph.get(x)){
int x2 = temp.getKey(), w2 = temp.getValue();
if(w+w2 &lt; dist[x2]){
dist[x2] = w + w2;
//Facing the error in below line
heap.add(new Pair&lt;Integer, Integer&gt;(w+w2, x2));
}
}
}
}
for(int i = 1; i &lt;= V; i++) {
if(i == S) continue;
if(dist[i] == Integer.MAX_VALUE)
System.out.print(&quot;-1 &quot;);
else
System.out.print(dist[i]+&quot; &quot;);
}
}
//Adding edge to a graph
public static void addEdge( ArrayList&lt;ArrayList&lt;Pair&lt;Integer, Integer&gt;&gt;&gt; graph, int u, int v, int w){
graph.get(u).add(new Pair&lt;Integer, Integer&gt;(v, w));
graph.get(v).add(new Pair&lt;Integer, Integer&gt;(u, w));
}
//Main Function
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t-- != 0) {
int n = in.nextInt();
int m = in.nextInt();
ArrayList&lt;ArrayList&lt;Pair&lt;Integer, Integer&gt;&gt;&gt; graph = new ArrayList&lt;&gt;();
for(int i = 0; i &lt;= n; i++) {
graph.add(new ArrayList&lt;Pair&lt;Integer, Integer&gt;&gt;());
}
for(int i = 0; i &lt; m; i++) {
addEdge(graph, in.nextInt(), in.nextInt(), in.nextInt());
}
int s = in.nextInt();
dijkstra(graph, n, m, s);
System.out.println();
}
}
}

I'm also adding the duplicates into the heaps as it doesn't affect my algorithm.

答案1

得分: 2

因为PriorityQueue维护其存储对象的排序顺序,为此需要一些对象之间的比较逻辑。

在您的情况下,对象是Pair,因此错误要求您要么使您的Pair类实现Comparable接口,要么为优先队列提供一个比较器来对Pair对象进行排序。

请查看这个链接:https://www.callicoder.com/java-priority-queue/

基本上,您需要提供自定义比较器。

英文:

Because PriorityQueue maintains sorted orders of the objects it stores, And for that it need some comparison logic between objects.

In your case it is Pair, so the error is asking you to either make your Pair class implement Comparable interface or provide priority queue a comparator to do the sorting of pair object.

Have a look at this https://www.callicoder.com/java-priority-queue/

Basically you have to provide custom comparator.

huangapple
  • 本文由 发表于 2020年8月6日 18:38:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63281800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定