我正在尝试从 java 优先队列算法导出到 C#。但是 Comparable<> 接口有问题
爪哇
public class MaxPQ<Key extends Comparable<Key>>
{
private Key[] pq; // heap-ordered complete binary tree
private int N = 0; // in pq[1..N] with pq[0] unused
public MaxPQ(int maxN)
{ pq = (Key[]) new Comparable[maxN+1]; }
//...
private bool less(int i, int j)
{ return pq[i].compareTo(pq[j]) < 0; }
}
C#
public class MaxPQ<Key> where Key : IComparable<Key>
{
private Key[] pq; // heap-ordered complete binary tree
private int N = 0; // in pq[1..N] with pq[0] unused
public MaxPQ(int maxN)
{ pq = new Key[maxN + 1]; }
//...
private bool less(int i, int j)
{ return pq[i].compareTo(pq[j]) < 0; }//Error
}
它抛出错误:“Key”不包含“compareTo”的定义,并且找不到接受“Key”类型的第一个参数的扩展方法“compareTo”(您是否缺少 using 指令或程序集引用?)
请问你能解决这个吗?!