2012-10-11 90 views
3

我需要幫助獲取一個類的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. 
+2

它是隊列迭代/循環您遇到的問題? –

回答

1

這就要你的方法,不多吧:

int totalDuration() { 
    int totalDuration = 0; 
    for (Job j : myJobs) totalDuration += j.getDuration(); 
    return totalDuration; 
} 

你也有一個totalDuration屬性,可以看到作爲緩存的總持續時間值。這應該只作爲一個必要的邪惡來引入,這是一個由於注意到的性能問題而導致的優化。如果這不是你的情況,請刪除該屬性。如果你真的需要它,因爲你當前的API(工種只添加),你可以保持通過更改addJob方法的總價值:

public void addJob(Job job) 
{ 
    myJobs.add(job); 
    totalDuration += job.getDuration(); 
} 

然後你需要的方法來獲得的總持續時間將只是

int totalDuration() { return totalDuration; } 

然而,當你的代碼的複雜性的增長,它通常變得越來越難以十個分量的總時間不變,有一個臨界點,你必須非常清楚你真正需要它的速度優勢。

+0

好點,但你也需要減去已刪除工作的持續時間。 – logoff

+0

@logoff我沒有注意到OP代碼中的刪除方法,我在我的答案中指出了這一點。 –

+0

這是真的!你已經指出了它。 – logoff

1

除非我誤解的目的,我不認爲你應該有一個totalDuration屬性爲JobQueue類。相反,創建一個名爲getTotalDuration的方法,並讓它循環遍歷myJob屬性中的所有Job,將每個單獨的JobjobDuration相加。

public int getTotalDuration() { 
    int totalDuration = 0; 
    for (Job job : this.myJobs) { 
     totalDuration += job.getDuration(); 
    } 
    return totalDuration; 
} 
2

做一個循環,以獲得總持續時間

public int getTotalDuration() { 
    int total = 0; 
    for(Job job : myJobs) { 
     total += job.getDuration(); 
    } 
    return total; 
} 
+0

+1你贏得了速度競賽,但是OP要求'Integer'結果,而不是'int' - 可能由於自動裝箱並不重要 – Bohemian

+2

但是有一個錯字,它應該是total + = .. .'而不是'result + = ...'。 – Koraktor

+1

@波希米亞人,他說'整數',而不是'整數'。 'int'是完美的響應,你不需要類包裝來存儲一個簡單的整數。即使他說'整數',我也更喜歡'int'。 – logoff

0
for (Job job : getPendingJobs()) 
{ 
    totalDuration += job.getDuration(); 
} 

這是所有

0

迭代ArrayList和總結每個作業實例的jobDuration。

for(Job j: jobList) { 
     //sum up all the durations here 
} 
1

如果希望所有作業的作業列表中的總時間,只需通過ArrayList中環,總結每個人任務的持續時間:

public int getDuration() 
{ 
    int totalDuration = 0; 
    for (Job job : myJobs) 
    { 
     totalDuration += job.getDuration(); 
    } 
    return totalDuration; 
} 

注意,這是一個簡單的解決問題的方法,因爲還有其他考慮可能對您很重要。例如,如果作業名稱是唯一的,則在遇到重複作業名稱時可能需要執行特殊行爲。例如,如果您想忽略重複項,您可以保留已經遇到的名稱的HashSet。無論如何,您需要確定此場景的期望行爲。

0

這可能有幫助。

//first class that contains jobs created. 
public class Job { 
    //fields 
    private String jobName; 
    private int jobDuration; 
    private static final int DEFAULT_DURATION = 0; 
    JobQueue queue = new JobQueue(); // Intialize the object.. Now queue.myJobs = a new array list of jobs. 

    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;    
     } 

     queue.addJob(this); 

    } 
    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 = 0; 
    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); 
     totalDuration += job.getDuration(); // It would be easier instead of looping through it, just to add to the total duration when the job is added to the queue. 
    } 

    public void getTotalDuration() { 
     return totalDuration; 
    } 
} 
0

更改您add方法

public void addJob(Job job) 
{ 
    myJobs.add(job); 
    totalDuration += job.getDuration(); 
} 

現在你有getTotalDuration()方法只是返回totalDuration.您將需要減去取出作業也。

public void getTotalDuration() { 
    return totalDuration; 
} 
1
public int getDuration(){ 

    int totalDuration=0; 

    for(Iterator<Job> it =myJobs.iterator(); it.hasNext();){ 
     Job job = it.next(); 
     totalDuration+=job.getDuration(); 
    } 

    return totalDuration; 

} 
相關問題