2012-10-13 52 views
0

我想創建一個運行隊列中所有作業的方法(作業列表)。我創建了一個運行一個作業的方法(runAJob),下面的方法是運行隊列中的所有作業,直到時間(myTimeLeft)用完。
現在,當我運行runAll方法時,此代碼始終保留1個ArraLyist。有人會告訴我爲什麼這是錯的?如何在隊列基本循環中打印字符串

/** 
    * 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(){ 
    myJobDuration = myJobInQueue.get(0).getDuration(); 
    if(!myJobInQueue.isEmpty()) 
    { 
     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() 
{ 
    int counAJob =0; 
    while (myJobDuration < myTimeLeft && !myJobInQueue.isEmpty()) { 
    // get next item from queue 
    runAJob(); 
    System.out.println("A job is running: "); 
    } 

} 
+0

'此代碼總是離開1 ArraLyist' - > Where ArrayList?你能發佈更多的代碼嗎?你的'runAJob()'方法,你的'myTimeLeft'定義在哪裏? –

+0

另外你在哪裏更改myTimeLeft? –

+0

@LewsTherin。可能在'runAJob()'方法中。 –

回答

0

爲什麼當你(大概)使用隊列時你有一個for循環?您不會根據隊列的大小做出任何決定(i)。我只是堅持讓隊列自己處理:

// declarations and definitions 

while (myJobDuration < myTimeLeft && !myJobInQueue.isEmpty()) { 
    // get next item from queue 
    runAJob(); 
}