简体   繁体   中英

Why is this giving me a bounds error?

I have a custom "Heap" class:

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


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

    }

and a custom "Process" class:

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;
    }

But if I try to make a new Heap of Processes, like Heap<Process> processHeap = new Heap<Process>(); I get the following error:

Bound mismatch: The type Process is not a valid substitute for the bounded parameter > of the type Heap

Why is this? I can't seem to figure it out.

流程未实现可比

The process object needs to implement the Comparable interface.

Example Implementation Comparing by Priority

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;
    }
}

Comparable API Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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