【发布时间】:2012-10-07 00:50:41
【问题描述】:
我被要求创建一个使用 Array List 实现作业队列的类。 我们可以将作业和运行时添加到队列中。作业从 JobInQueue 列表中队列的前面运行,myPendingTime 被减去,完成的作业进入 Finishedjobs 列表。
看起来我们必须为此使用 boolean mutator 方法,但我不确定如何创建此方法。 谁能告诉我该怎么做。
/**
* runs the first job on the queue if there is enough time on the clock.
*/
public boolean runJob()
{
boolean jobDone = runJob();
if(myJobInQueue.isEmpty() && myDuration > myPendingTime){
myDuration-= myPendingTime;
myJobInQueue.remove(0);
myFinishedJobs.add(myJobInQueue.get(0));
System.out.println("The job runing is : "+ myJobInQueue.get(0));
jobDone=true;
}
return jobDone ;
}
【问题讨论】:
-
你的第一行方法将在
Maximum Recursion Depth..中创建无限递归结果。 -
您正在从队列中删除作业,然后再放入已完成的队列。它应该相反,即
myJobInQueue.remove(0);应该在myFinishedJobs.add(myJobInQueue.get(0));之后。 -
使方法返回为
void,从顶部更改boolean jobDone = false;,并将此if(!jobDone){ runJob(); }放在底部。删除return jobDone行。 -
感谢您的回答,我已经替换了 myJobInQueue...现在如何返回布尔值?