亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取 ArrayLists IndexOutBoundsException

獲取 ArrayLists IndexOutBoundsException

慕妹3242003 2024-01-28 16:44:24
我正在嘗試用java創建一個進程調度程序。我有一個示例進程類和一個調度程序類,我在其中執行調度邏輯。我在先到先服務器 (FCFS) 方法的第一行遇到錯誤。Queue.add(Arriving.get(0));線程“main”中的異常 java.lang.IndexOutOfBoundsException:索引:0,大?。? 在 java.util.ArrayList.rangeCheck(未知來源) 在 java.util.ArrayList.get(未知來源)public class Scheduler {    // I use two lists to keep track of the processes that haven't arrived and the processes in the cpu queue    ArrayList<Process> Arriving;    ArrayList<Process> Queue;    Process runningProcess;    int currentTime;    // new process boolean used to check if a process was added to the cpu queue    boolean newProcess;    public Scheduler(ArrayList<Process> Queue){        Arriving = new ArrayList<Process>();        Queue = new ArrayList<Process>();        for (int i = 0; i<Queue.size(); i++) {            Arriving.add(Queue.get(i));        }        Sort();        currentTime = 0;    }    public void FCFS(){        Queue.add(Arriving.get(0));        Arriving.remove(0);        runningProcess = Queue.get(0);        while(runningProcess.getRemainingTime() != 0){            if (runningProcess.getRemainingTime()==0){                Queue.remove(runningProcess);                runningProcess = Queue.get(0);            }            while (Arriving.get(0) != null){                for(Process process:Arriving){                    if (process.getArrivalTime()==currentTime){                        Queue.add(Arriving.get(0));                        Arriving.remove(0);                    }                    else                        break;                }                runningProcess.running();                for(int i = 1; i<Queue.size(); i++)                    Queue.get(i).waiting();            }            System.out.println(Queue.size() + " processes waiting.");            currentTime++;        }    }
查看完整描述

3 回答

?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

在執行之前添加一個空列表檢查Queue.add(Arriving.get(0));。

那應該可以解決問題。

僅當 List 中存在某些內容時,才應該執行 get() 或 remove() 操作。


查看完整回答
反對 回復 2024-01-28
?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

在 main 方法的代碼中,您初始化,并向其中queue添加 的實例。Process


ArrayList<Process> queue = new ArrayList<Process>();

queue.add(a);

queue.add(b);

queue.add(c);

queue.add(d);

queue.add(e);

Scheduler run = new Scheduler(queue);

queue被傳遞到 的構造函數中Scheduler,只是再次初始化,從而刪除了Process之前在其中的所有實例。


public Scheduler(ArrayList<Process> Queue) {

    Arriving = new ArrayList<Process>();

    Queue = new ArrayList<Process>(); // Change this line to this.Queue = Queue

    for (int i = 0; i<Queue.size(); i++) {

        Arriving.add(Queue.get(i));

    }

    Sort();

    currentTime = 0;

}

因此,當您嘗試循環構造函數中的所有對象時,Queue.size()將返回 0。


您ArrayList<Process> Queue作為該類的成員,盡管該名稱反映了傳遞Queue到Scheduler.


您可以簡單地設置,而不是循環遍歷并將Queue所有對象添加到。ArrivingArriving = Queue


查看完整回答
反對 回復 2024-01-28
?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

您正在使用 ArrayList 作為隊列。myList.isEmpty()在訪問元素之前應該檢查空隊列的測試。

或者,您可以使用,當您使用或java.util.Deque查看空雙端隊列的頭部或尾部時,它會返回 null 。peekFirst()peekLast()


查看完整回答
反對 回復 2024-01-28
  • 3 回答
  • 0 關注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號