我需要幫助獲取一個類的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.
它是隊列迭代/循環您遇到的問題? –