我需要帮助获取一个类的ArrayList的元素并在另一个类中使用它。从一个类的ArrayList中获取要用于另一个类的元素
该任务基本上是分配Job类中输入的任务,该类放置在JobQueue类的数组列表中。我现在的问题是我有一个持续时间作为Job类中每个任务的整数,并且我试图获得JobQueue类中使用的总持续时间,如果有超过1个任务。
任务: - 在JobQueue类中使用的Job类中获取作业的总运行时间。
这是我到目前为止。
//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.
它是队列迭代/循环您遇到的问题? –