我有一个Processes
的arraylist,我想根据他们的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) {
任何人都可以解释它的内容吗?
答案 0 :(得分:2)
我不确定错误消息是否是我所期望的,但您的compare
方法无法降低界面中定义的方法的可见性。
将compare
声明为public
,它应该有效(当然你摆脱了毫无意义和错误的equals
方法之后)。
Collections.sort(pArray, new Comparator<Process>(){
public int compare(Process o1, Process o2) {
return o1.getAt() - o2.getAt();
}
});