我正在尝试编写一个简单的CPU调度程序。 (确实如此,但不再是家庭作业,我只是想了解它。)
作为其中的一部分,我将对象添加到ArrayList,然后将其移动到另一个ArrayList。 但是,我遇到了两个问题:
我查看了ArrayList API并完成了对stackoverflow的搜索,但还没有找到有用的解决方案。
相关代码如下(我已经修剪了一些我认为不相关的内容,并且有几个占位符变量,因为这是完成中期):
public class Tasks {
public static ArrayList<PCB>jobQueue = new ArrayList<>();
public static ArrayList<PCB>readyQueue = new ArrayList<>(3);
public ArrayList<Integer>tempCPU = new ArrayList<>();
public ArrayList<Integer>tempIO = new ArrayList<>();
public int nextDigit, a, b, c, d, e, f = 0;
public int jobCount = 0;
//method to read in text from text file (trimmed)
public void fileInput(String filename){
while (fileInput.hasNextLine()){
String line = fileInput.nextLine();
numberInput(line);
}
}
//method to parse the input data, create a PCB class object using it,
//and add it to the jobQueue ArrayList
public void numberInput(String line){
//Add PCB object (job) to jobQueue ArrayList
jobQueue.add(new PCB(tempCPU, tempIO, jobCount, jobCount, c, d, e, f));
//clear temporary ArrayLists (filled earlier in
//the method with now-trimmed code)
tempCPU.clear();
tempIO.clear();
}
}//end Tasks class
public class PCB {
ArrayList<Integer> CPUBurstProcess = new ArrayList<Integer>();
ArrayList<Integer> IOBurstProcess = new ArrayList<Integer>();
//object variables
int processID;
int CPUBurstIndex; //= 1; //assign these in the constructor
int IOBurstIndex; //= 1;
int CPUBurstTimeRemaining;
int IOBurstTimeRemaining;
int PCBPointer; //= 0;
public boolean ReadyOrDisk = true; //T for ReadyQueue, F for DiskQueue
public PCB(ArrayList<Integer> CPUBurstProcess,
ArrayList<Integer> IOBurstProcess, int processID, int CPUBurstIndex,
int IOBurstIndex, int CPUBurstTimeRemaining, int IOBurstTimeRemaining,
int PCBPointer) {
this.CPUBurstProcess = CPUBurstProcess;
this.IOBurstProcess = IOBurstProcess;
this.processID = processID;
this.CPUBurstIndex = 1;
this.IOBurstIndex = 1;
this.CPUBurstTimeRemaining = CPUBurstTimeRemaining;
this.IOBurstTimeRemaining = IOBurstTimeRemaining;
this.PCBPointer = 0;
}
}//end PCB class
如果它有用,我很乐意展示更多的代码或输出数据。我觉得这很简单,我不理解,但我已阅读了许多似乎有类似问题的主题,并且没有适用的建议/解决方案。
提前感谢您的帮助!
编辑:
我尝试使用(stackoverflow.com/questions/19843506 / ...)中的建议:静态变量,并将它们更改为非静态变量/ ArrayLists(上面更新的代码匹配)。
但是,我仍然看到相同的覆盖问题,并且不认为我两次添加相同的对象。在该线程中,对于我所遇到的突然空的ArrayList问题也没有任何帮助。