JAVA 为什么我的对象类型数组元素不接受我分配的所有属性值?



我当前正在使用Java语言,数组和队列编码。我在使用"过程"类型的数组时遇到了问题。我正在阅读一个具有以下内容的输入文件:

3 1 5 30 3 1 5 30 4 0 5 30 3

这是堆栈。当您看到我的控制台时,您会看到每个元素(=过程类型的过程对象)正确地接收其所有属性(a,b,c,d),当我将它们添加到AllProcesses数组中时。

但是,当我使用printProcesses()方法打印出AllProcesses数组时,每个进程对象的" A"属性都是0-> allProcess [p] .a = 0,而应该为1。

根据请求,以下是fcfs()和进程类。

//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;
}
}

我试图使用我的调试器找到错误,但我所弄清的是,该过程对象没有采用其最初给出的"属性值。这是在printProcesses()中显示第一个" curelement"属性的调试窗口。" A"属性只是缺少。其他两个条件也是如此。

请告知我如何解决此问题,并让我知道是否还有其他信息可以使您更容易。预先感谢。

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("noriginaln");
        printProcesses(allProcesses, numProcesses); //original
        FCFS(allProcesses, numProcesses);
        System.out.println();
        System.out.printf("sortedn");
        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("nn");
}

您有static int a;。因此,它将具有最后更新,而您的情况为零。而且,您没有在调试器中获得a的原因很可能是您的设置。Eclipse的调试菜单中应该有

最新更新