第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

獲取 ArrayLists IndexOutBoundsException

獲取 ArrayLists IndexOutBoundsException

慕妹3242003 2024-01-28 16:44:24
我正在嘗試用java創(chuàng)建一個(gè)進(jìn)程調(diào)度程序。我有一個(gè)示例進(jìn)程類和一個(gè)調(diào)度程序類,我在其中執(zhí)行調(diào)度邏輯。我在先到先服務(wù)器 (FCFS) 方法的第一行遇到錯(cuò)誤。Queue.add(Arriving.get(0));線程“main”中的異常 java.lang.IndexOutOfBoundsException:索引:0,大小:0 在 java.util.ArrayList.rangeCheck(未知來(lái)源) 在 java.util.ArrayList.get(未知來(lái)源)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貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊

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

那應(yīng)該可以解決問(wèn)題。

僅當(dāng) List 中存在某些內(nèi)容時(shí),才應(yīng)該執(zhí)行 get() 或 remove() 操作。


查看完整回答
反對(duì) 回復(fù) 2024-01-28
?
慕村225694

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊

在 main 方法的代碼中,您初始化,并向其中queue添加 的實(shí)例。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被傳遞到 的構(gòu)造函數(shù)中Scheduler,只是再次初始化,從而刪除了Process之前在其中的所有實(shí)例。


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;

}

因此,當(dāng)您嘗試循環(huán)構(gòu)造函數(shù)中的所有對(duì)象時(shí),Queue.size()將返回 0。


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


您可以簡(jiǎn)單地設(shè)置,而不是循環(huán)遍歷并將Queue所有對(duì)象添加到。ArrivingArriving = Queue


查看完整回答
反對(duì) 回復(fù) 2024-01-28
?
慕運(yùn)維8079593

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊

您正在使用 ArrayList 作為隊(duì)列。myList.isEmpty()在訪問(wèn)元素之前應(yīng)該檢查空隊(duì)列的測(cè)試。

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


查看完整回答
反對(duì) 回復(fù) 2024-01-28
  • 3 回答
  • 0 關(guān)注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)