0

我有一个自定义的“堆”类:

public class Heap<T extends Comparable<T>>
{
    ArrayList<T> heapList;


    public Heap()
    {
        heapList = new ArrayList<T>();

    }

和一个自定义的“进程”类:

public class Process {
    private int processID, timeUnitsRequired, priority, timeOfArrival;

    public Process(int processID, int timeUnitsRequired, int priority, int timeOfArrival) {
        this.processID = processID;
        this.timeUnitsRequired = timeUnitsRequired;
        this.priority = priority;
        this.timeOfArrival = timeOfArrival;
    }

但是如果我尝试创建一个新的进程堆,就像Heap<Process> processHeap = new Heap<Process>();我得到以下错误一样:

绑定不匹配:类型 Process 不是 Heap 类型的有界参数的有效替代品

为什么是这样?我似乎无法弄清楚。

4

2 回答 2

2

流程未实现 Comparable

于 2012-11-25T23:34:44.717 回答
1

进程对象需要实现Comparable接口。

示例实现按优先级进行比较

public class Process implements Comparable<Process>{

    @Override
    public int compareTo(Object o) {
        Process p2 = (Process) o;
        if(this.priority > p2.priority){
          return 1;
        }else if(this.priority < p2.priority){
          return -1;
        }
        return 0;
    }
}

可比较的 API 文档

于 2012-11-25T23:37:15.990 回答