简体   繁体   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

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.我正在为加权图编写 Dijkstra 算法。 I used ArrayList<ArrayList<Pair<Integer, Integer>>> for storing my weights and edges.我使用 ArrayList<ArrayList<Pair<Integer, Integer>>> 来存储我的权重和边。 While the implementation of the Dijkstra function, I used MinHeap which is PriorityQueue in java of type Pair<Integer, Integer>.在实现 Dijkstra function 时,我使用了 MinHeap,它是 Pair<Integer, Integer> 类型的 java 中的 PriorityQueue。 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<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();
        }
    }

}

I'm also adding the duplicates into the heaps as it doesn't affect my algorithm.我还将重复项添加到堆中,因为它不会影响我的算法。

Because PriorityQueue maintains sorted orders of the objects it stores, And for that it need some comparison logic between objects.因为 PriorityQueue 维护它存储的对象的排序顺序,并且为此它需要一些对象之间的比较逻辑。

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

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

Basically you have to provide custom comparator.基本上你必须提供自定义比较器。

暂无
暂无

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

相关问题 线程“主”java.lang.ClassCastException 中的异常:setcollection.Enseignant 无法转换为 java.lang.Comparable - Exception in thread “main” java.lang.ClassCastException: setcollection.Enseignant cannot be cast to java.lang.Comparable 线程“主”java.lang.ClassCastException 中的异常:proj.Car 无法转换为 java.lang.Comparable - Exception in thread "main" java.lang.ClassCastException: proj.Car cannot be cast to java.lang.Comparable 错误:“线程中的异常”main“java.lang.ClassCastException:manycard.Main$Card 无法转换为 java.lang.Comparable” - Error: “Exception in thread ”main“ java.lang.ClassCastException: manycard.Main$Card cannot be cast to java.lang.Comparable” java.lang.ClassCastException: 不能转换为 java.lang.Comparable - java.lang.ClassCastException: cannot be cast to java.lang.Comparable java.lang.ClassCastException:java.util.HashMap无法强制转换为java.lang.Comparable - java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.Comparable java.lang.ClassCastException:尝试排序List时,java.util.LinkedHashMap无法转换为java.lang.Comparable异常 - java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.lang.Comparable exception when trying sort a List java.lang.ClassCastException: org.postgresql.util.PGobject 不能转换为 java.lang.Comparable; UUID转换器内部 - java.lang.ClassCastException: org.postgresql.util.PGobject cannot be cast to java.lang.Comparable; inside UUIDconverter Java 运行时错误:java.lang.ClassCastException:main.Scrapper$aktie 无法转换为 java.lang.Comparable - Java runtime error: java.lang.ClassCastException: main.Scrapper$aktie cannot be cast to java.lang.Comparable 列表到TreeSet的转换产生:“ java.lang.ClassCastException:MyClass无法转换为java.lang.Comparable” - List to TreeSet conversion produces: “java.lang.ClassCastException: MyClass cannot be cast to java.lang.Comparable” MyClass无法强制转换为java.lang.Comparable:java.lang.ClassCastException - MyClass cannot be cast to java.lang.Comparable: java.lang.ClassCastException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM