1

我有一个数组Processes列表,我想根据它们对它们进行排序arrival time,问题是我似乎无法Comparator很好地编码。下面是我的代码:

ArrayList<Process> pArray = new ArrayList<>();
            for(int x = 0; x<processTable.getRowCount(); x++){
                int pID = Integer.parseInt(processTable.getValueAt(x,0).toString());
                int aT = Integer.parseInt(processTable.getValueAt(x,1).toString());
                int bT = Integer.parseInt(processTable.getValueAt(x,2).toString());
                Process temp = new Process(bT, aT, pID);
                totalBT += bT;
                pArray.add(temp);
            }
            //sort by arrival time
            Collections.sort(pArray, new Comparator<Process>(){
                    int compare(Process o1, Process o2) {
                        return o1.getAt() - o2.getAt();
                    }
                    boolean equals(Process obj) {

                    }
                });

            try{
                System.out.print("ha");
                pArray = doRR(new Integer(rr1Q.getValue().toString()), pArray, totalBT);

            }catch(InterruptedException ie){
                    System.out.println("Process ended due to interruption");
            }

弹出以下错误:

compare(Process,Process) in <anonymous my.CpuGui.CpuGui$ButtonHandler$1> cannot implement compare(T,T) in Comparator
                    int compare(Process o1, Process o2) {

谁能解释它说什么?

4

1 回答 1

2

我不确定错误消息是我所期望的逐字记录,但是您的compare方法不能降低接口中定义的方法的可见性。

声明comparepublic它应该可以工作(当然,在你摆脱毫无意义和错误equals的方法之后)。

Collections.sort(pArray, new Comparator<Process>(){
    public int compare(Process o1, Process o2) {
        return o1.getAt() - o2.getAt();
    }
});
于 2013-09-30T04:23:58.660 回答