为了账号安全,请及时绑定邮箱和手机立即绑定

获取 ArrayLists IndexOutBoundsException

获取 ArrayLists IndexOutBoundsException

慕妹3242003 2024-01-28 16:44:24
我正在尝试用java创建一个进程调度程序。我有一个示例进程类和一个调度程序类,我在其中执行调度逻辑。我在先到先服务器 (FCFS) 方法的第一行遇到错误。Queue.add(Arriving.get(0));线程“main”中的异常 java.lang.IndexOutOfBoundsException:索引:0,大小: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 关注
  • 50 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信