获取ArrayLists IndexOutBoundsException

时间:2019-11-01 22:28:59

标签: java eclipse scheduling

我正在尝试在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++;
        }
    }

public class Main {

    public static void main(String[] args) {
        Process a = new Process(1, 15, 0, 0);
        Process b = new Process(2, 9, 5, 0);
        Process c = new Process(3, 3, 8, 0);
        Process d = new Process(4, 12, 10, 0);
        Process e = new Process(5, 4, 14, 0);
        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);
        run.FCFS();
}
        public void Sort(){
        ArrayList<Process> Unsorted = new ArrayList<Process>();
        for (int i = 0; i < Arriving.size(); i++){
            Unsorted.add(Arriving.get(i));
        }
        ArrayList<Process> Sorted = new ArrayList<Process>();
        Process someProcess;
        for(int i = 0; i<Unsorted.size(); i++) {
            someProcess = Unsorted.get(i);
            for (Process process:Unsorted){
                if (process.getArrivalTime()<someProcess.getArrivalTime())
                    someProcess = process;
            }
            Sorted.add(someProcess);
            Unsorted.remove(someProcess);
        }
        Arriving = new ArrayList<Process>();
        for (Process process:Sorted) {
            Arriving.add(process);
        }
    }
}

3 个答案:

答案 0 :(得分:0)

在您的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而不是遍历Arriving并将所有对象添加到Arriving = Queue中。

答案 1 :(得分:0)

您将ArrayList用作队列。空队列的测试是myList.isEmpty(),您应该在访问元素之前对其进行检查。

或者,您可以使用java.util.Deque,当您使用peekFirst()peekLast()来查看空双端队列的头部或尾部时,它将返回null。

答案 2 :(得分:0)

在执行Queue.add(Arriving.get(0));之前添加一个空列表检查。

那应该可以解决问题。

仅当列表中存在某些内容时,才应执行get()或remove()。