Java 如何在队列基本循环中打印字符串
我想创建一个运行队列(作业列表)中所有作业的方法。我创建了一个方法来运行一个作业(Java 如何在队列基本循环中打印字符串,java,Java,我想创建一个运行队列(作业列表)中所有作业的方法。我创建了一个方法来运行一个作业(runAJob),下面的方法是运行队列列表中的所有作业,直到时间(myTimeLeft)用完为止。 现在,当我运行runAll方法时,这段代码总是留下一个数组列表。有人能告诉我为什么错了吗 /** * Run the first job on the queue if there is enough time on the clock and the job queue list is not empty
runAJob
),下面的方法是运行队列列表中的所有作业,直到时间(myTimeLeft)用完为止。现在,当我运行
runAll
方法时,这段代码总是留下一个数组列表。有人能告诉我为什么错了吗
/**
* Run the first job on the queue if there is enough time on the clock and the job queue list is not empty.
* And move the job to the finished jobs list.
*/
public void runAJob(){
myJobDuration = myJobInQueue.get(0).getDuration();
if(!myJobInQueue.isEmpty())
{
if (myJobDuration < myTimeLeft)
{
myTimeLeft = myTimeLeft - myJobDuration;
myFinishedJobs.add(myJobInQueue.get(0));
System.out.println("A job is running: " + myJobInQueue.get(0).getName());
myJobInQueue.remove(0);
}
else
{
System.out.println("Not enogth running time left, please add time on the clock.");
}
}
else
{
System.out.println("No pending job on the list.");
}
}
/**
* Run all the jobs on the queue in order until it runs out of time.
*/
public void runAll()
{
int counAJob =0;
while (myJobDuration < myTimeLeft && !myJobInQueue.isEmpty()) {
// get next item from queue
runAJob();
System.out.println("A job is running: ");
}
}
/**
*如果时钟上有足够的时间且作业队列列表不为空,则运行队列上的第一个作业。
*并将作业移动到“已完成作业”列表。
*/
公共无效runAJob(){
myJobDuration=myJobInQueue.get(0.getDuration();
如果(!myJobInQueue.isEmpty())
{
如果(myJobDuration
当您(大概)使用队列时,为什么会有for循环?您不会根据队列的大小(i
)做出任何决定。我会坚持让队列自行处理:
// declarations and definitions
while (myJobDuration < myTimeLeft && !myJobInQueue.isEmpty()) {
// get next item from queue
runAJob();
}
//声明和定义
while(myJobDuration
此代码始终保留1个ArrayList
->ArrayList在哪里?你能再发一些代码吗?您的runAJob()
方法以及您的myTimeLeft
在哪里定义?您在哪里更改myTimeLeft?@lewsterin。可能在runAJob()
方法中。myTimeLeft
和myJobDuration
可能是问题所在,它们是如何定义的?什么时候更改?我已经重新发布了我的问题。这次我加入了runAJob方法。但这仍然不起作用。谁能告诉我怎么了?