gpt4 book ai didi

java - 如何打印出列表中的一个元素以及所有元素

转载 作者:行者123 更新时间:2023-12-01 07:35:21 28 4
gpt4 key购买 nike

当我尝试运行(使用)最后一个方法时,它开始运行并且永远不会停止。另外,当我尝试打印 aJob 时,该元素无法正确打印。它带有奇怪的字母。我发布整个类(class)是因为我猜测除了 runJod 和 runAll 方法之外,我在其他地方犯了错误。谁能告诉我需要做什么来解决这个问题?

导入java.util.ArrayList;

/** */

公共(public)类JobQueue

{ 私有(private)ArrayListmyJobInQueue;//要完成的工作列表

private ArrayList<Job>myFinishedJobs;// a list of compleated job

private int myJobDuration; //duration if one job

private int myTimeLeft;//total time left
/**
* Constructor for objects of class JobQueue
*/
public JobQueue()
{
myJobInQueue = new ArrayList<Job>();
myFinishedJobs = new ArrayList<Job>();
myJobDuration =0;
myTimeLeft=0;
}

/**
* Return the list of jobs that have not been completed (including the current job).
*/
public ArrayList<Job> getPendingJobs()
{
return myJobInQueue;
}

/**
* Return the list of jobs that have been completed.
*/
public ArrayList<Job> getCometedJobs()
{
return myFinishedJobs;
}

/**
* Return the job at the front of the pending queue, or null if the queue is empty.
*/
public Job getCurrentJob()
{
if(myJobInQueue!=null)
{
Job FirstJobInTheQueue = myJobInQueue.get(0);
return FirstJobInTheQueue;
}
else
{
return null;
}

}

/**
* Return the amount of time left on the clock (as an integer)
*/
public int getTimeLeft()//Ok
{
return myTimeLeft;
}

/**
* Return the total duration of all the pending jobs(as an integer).
*/

public int getTotalDuration()
{
int myTimeLeft= 0;
for(int i = 0; i<myJobInQueue.size();i++)
{
int num = myJobInQueue.getDuration(i); //I think this line is wrong.
myTimeLeft = myTimeLeft + num ;
}
return myTimeLeft;
}

/**
* Add a Job to the end of the Queue
*/
public void addJob(Job job)
{
if(job!=null)
{
myJobInQueue.add(job);
}
}

/**
* Add the specified number of seconds to the clock.
*/
public void addTime(int seconds)
{
if(seconds>0)
{
myTimeLeft = myTimeLeft + seconds;
}
}

/**
* 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(){
if(!myJobInQueue.isEmpty())
{
myJobDuration = myJobInQueue.get(0).getDuration();
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()
{
for(int i = 0; myTimeLeft > 0 && myTimeLeft > myJobDuration;i++);
{
runJob();

}
System.out.println("Job can not be run, not enough time left." );
}

}

最佳答案

第 1 点:myJobInQueue.get(0)未打印正确的值。

如果您的 JobtoString() 方法,正如 Bill 在他的回答中提到的那样,这将打印正确的值。另一种方法是调用 Job 类的 getter 方法(如果有),例如

myJobInQueue.get(0).getJobName();

第二点:调试它并查看 myTimeLeft 的值是否真的在任何时候都低于零。

关于java - 如何打印出列表中的一个元素以及所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12819356/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com