繁体   English   中英

线程“主”java.lang.ClassCastException 中的异常:javafx.util.Pair 无法转换为 java.lang.Comparable

[英]Exception in thread “main” java.lang.ClassCastException: javafx.util.Pair cannot be cast to java.lang.Comparable

另外,这个问题已经问了很多次,根据我的解决方案我无法弄清楚。 我正在为加权图编写 Dijkstra 算法。 我使用 ArrayList<ArrayList<Pair<Integer, Integer>>> 来存储我的权重和边。 在实现 Dijkstra function 时,我使用了 MinHeap,它是 Pair<Integer, Integer> 类型的 java 中的 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;

      //Facing the error in below line

                        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]+" ");
        }
    }
//Adding edge to a graph
    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));
    }
//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<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();
        }
    }

}

我还将重复项添加到堆中,因为它不会影响我的算法。

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

在您的情况下,它是 Pair,因此错误是要求您让您的 Pair class 实现 Comparable 接口或提供优先级队列比较器来对 object 进行排序。

看看这个https://www.callicoder.com/java-priority-queue/

基本上你必须提供自定义比较器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM