[英]JAVA Why don't my object-type array elements take in all the property values that I assigned?
I am currently coding using JAVA language, an array and a queue. 我目前正在使用JAVA语言,数组和队列进行编码。 I am having a problem with using an array that is a "Process" type. 我在使用“Process”类型的数组时遇到问题。 I am reading an input file which have the following: 我正在读取一个输入文件,其中包含以下内容:
3 1 5 30 3 1 5 30 4 0 5 30 3 3 1 5 30 3 1 5 30 4 0 5 30 3
Here is the stacktrace. 这是堆栈跟踪。 When you see my console, you can see that each element (=Process type process object) takes in all of its properties (a, b, c, d) properly, when I am adding each of them into allProcesses array. 当您看到我的控制台时,您可以看到每个元素(=进程类型进程对象)正确地接受其所有属性(a,b,c,d),当我将它们中的每一个添加到allProcesses数组中时。
However, when I print out allProcesses array USING the printProcesses() method, the "a" property of each Process objects are all 0 --> allProcess[p].a = 0 whereas it should be 1. 但是,当我使用printProcesses()方法打印allProcesses数组时,每个Process对象的“a”属性都是0 - > allProcess [p] .a = 0,而它应该是1。
Upon the requests, here are FCFS(), and Process class. 根据请求,这里是FCFS()和Process类。
//Sorts "allProcesses" in an ascending order of "a".
public static void FCFS (Process[] allProcesses, int numProcesses) {
ArrayList<Process> FCFSsortedAllProcesses = new ArrayList<Process>();
// Iterating through the created list from the position
for (int p = 0; p < allProcesses.length; p++) {
for (int j = p + 1; j < allProcesses.length; j++) {
if (allProcesses[p].a > allProcesses[j].a) {
Process temp = allProcesses[p];
allProcesses[p] = allProcesses[j];
allProcesses[j] = temp;
}
}
}
}
public class Process {
/* Linked list Node*/
int id;
static int a;
int b;
int c;
int io;
int readyCycle;
int CPUburstRemaining;
int CPUcycle;
int IOburstRemaining;
int IOcycle;
String state;
String relationship;
int priority;
//int runningTime;
int finishingTime;
int turnaroundTime;
int ioTime;
int waitingTime;
Process next;
//Node prev;
// Constructor to create a new node
// Next is by default initialized as null
public Process(int a, int b, int c, int io){
this.id = id;
this.a = a;
this.b = b;
this.c = c;
this.io = io;
this.readyCycle = readyCycle;
this.CPUburstRemaining = 0;
this.CPUcycle = 0;
this.IOburstRemaining = 0;
this.IOcycle = 0;
this.state = "unstarted";
this.relationship = null;
this.priority = 0;
this.finishingTime = 0;
this.turnaroundTime = 0;
this.ioTime = 0;
this.waitingTime = 0;
}
}
I tried to find the error using my debugger, but all I figured out is that the Process object doesn't take its "a" property value that was originally given. 我试图使用我的调试器找到错误,但我想到的是Process对象没有采用最初给出的“a”属性值。 Here is the debugger window that shows the properties of the first "curElement" in printProcesses(). 这是调试器窗口,显示printProcesses()中第一个“curElement”的属性。 The "a" property is just missing. “a”属性遗失了。 The same goes for the other two curElements as well. 其他两个curElements也是如此。
Please advise me on how to fix this issue, and let me know if there is any other information I should provide to make it easier for you. 请告诉我如何解决这个问题,并告诉我是否有任何其他信息我应该提供给您更容易。 Thanks in advance. 提前致谢。
public static void main(String args[]) {
try {
String fileAddress = args[0];
File fileInput = new File(fileAddress); //Read
Scanner scan = new Scanner(fileInput);
int numProcesses = scan.nextInt();
Queue<Process> processes = new LinkedList<Process>();
Process[] allProcesses = new Process[numProcesses];
//Adding each process to processes queue
for (int m = 0; m < numProcesses; m++) {
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
int io = scan.nextInt();
Process thisProcess = new Process(a, b, c, io);
thisProcess.id = m;
processes.add(thisProcess);
allProcesses[m] = thisProcess;
System.out.println(m + " thisProcess.a = " + thisProcess.a);
System.out.println(m + " allProcesses[m].a = " + allProcesses[m].a);
}
System.out.printf("\noriginal\n");
printProcesses(allProcesses, numProcesses); //original
FCFS(allProcesses, numProcesses);
System.out.println();
System.out.printf("sorted\n");
printProcesses(allProcesses, numProcesses); //sorted
}
catch (Exception e){
e.printStackTrace();
System.out.printf(" Error: File not foundd. \n");
}
}
public static void printProcesses (Process[] allProcesses, int numProcesses) {
System.out.printf("The original input was: ");
for (int p = 0; p < allProcesses.length; p++) {
Process curElement = allProcesses[p];
System.out.printf("%d %d %d %d ", curElement.a, curElement.b, curElement.c, curElement.io);
}
System.out.print("\n\n");
}
You have static int a;
你有static int a;
. 。 So it will have the last update which in your case is zero. 所以它会有最后一次更新,在你的情况下为零。 And the reason you are not getting a
in debugger is most likely your settings. 其原因你没有得到a
调试器是最有可能您的设置。 There should be show static variables in your debug menu for eclipse. 在Eclipse的调试菜单中应该显示静态变量 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.