我需要帮助来获取一个类的 ArrayList 的元素并在另一个类中使用它。
该任务基本上是分配在 Job 类中输入的任务,该任务放置在 JobQueue 类的 arraylist 中。我目前遇到的问题是,对于 Job 类中的每个任务,我都有一个整数作为整数,如果输入的任务超过 1 个,我试图获取 JobQueue 类中使用的总持续时间。
任务:- 获取 Job 类中要在 JobQueue 类中使用的作业的总持续时间。
这是我到目前为止所拥有的。
//first class that contains jobs created.
public class Job
{
//fields
private String jobName;
private int jobDuration;
private static final int DEFAULT_DURATION = 0;
public Job(String task, int duration)
{
//constructor used to create the job name and how long it will take to do one job (duration).
jobName = task;
if (duration > DEFAULT_DURATION)
{
jobDuration = duration;
}
else
{
jobDuration = DEFAULT_DURATION;
}
}
public String getName()
{
return jobName;
}
public int getDuration()
{
return jobDuration;
}
}
// 2nd class used to enter every job created into an Arraylist called myJobs.
public class JobQueue
{
private ArrayList <Job> myJobs;
private int totalDuration;
private static final int DEFAULT_NUM = 0;
public JobQueue()
{
myJobs = new ArrayList<Job>();
totalDuration = DEFAULT_NUM;
}
public ArrayList<Job> getPendingJobs()
{
return myJobs;
}
public void addJob(Job job)
{
myJobs.add(job);
}
}
//here i need a method that gets the total duration of jobs in the arraylist and returns total as an integer. Please help.