import java.util.*;
class Huffman {
int a;
public Huffman(int i)
{
a = i;
}
}
class PriorityQueueDemo implements Comparator<Huffman>
{
public int compare(Huffman ab, Huffman cd)
{
if(ab.a>cd.a)
{
return 1;
}
return 0;
}
public static void main(String[] args)
{
PriorityQueue<Huffman> HuffQueue = new PriorityQueue<Huffman>();
Huffman ab = new Huffman(10);
HuffQueue.offer(ab);
ab = new Huffman(50);
HuffQueue.offer(ab);
ab = new Huffman(20);
HuffQueue.offer(ab);
ab = new Huffman(60);
HuffQueue.offer(ab);
while(HuffQueue.size() > 0)
System.out.println(HuffQueue.remove());
}
}
以下代码有什么问题!它生成以下异常:
Exception in thread "main" java.lang.ClassCastException: Huffman cannot be cast to java.lang.Comparable
at java.util.PriorityQueue.siftUpComparable(Unknown Source)
at java.util.PriorityQueue.siftUp(Unknown Source)
at java.util.PriorityQueue.offer(Unknown Source)
at PriorityQueueDemo.main(Huffman.java:34)