我已經用Java編寫了一個模擬程序,但運行時遇到了一些問題。我爲模擬模型的各個方面而創建的自定義類似乎從未初始化過。例如,爲什麼我創建的對象處於null狀態?
public class Queue {
public static int front = -1;
public static int length = 0;
public static int capacity = 0;
public static int roadqueue[];
public static int position[];
public static boolean full = false;
public static void push(int pushValue) {
然後可以從類運行的功能。
在我的主要項目中,我聲明類如下:
public class myProject {
public static Queue queue[] = new Queue[33];
,然後在主函數中,我調用了應該在這個類初始化值功能的第一件事:
public static void main(String[] args) {
initQueue();
與
public static void initQueue() {
queue[0].length = 6000;
queue[14].length = 6000;
queue[1].length = 20;
queue[13].length = 20;
queue[3].length = 10;
queue[11].length = 10;
queue[4].length = 360;
queue[12].length = 360;
queue[5].length = 260;
queue[10].length = 260;
queue[6].length = 20;
queue[9].length = 400;
queue[15].length = 460;
queue[22].length = 460;
queue[16].length = 260;
queue[21].length = 260;
queue[17].length = 100;
queue[26].length = 100;
queue[18].length = 160;
queue[20].length = 140;
queue[23].length = 240;
queue[25].length = 140;
queue[27].length = 80;
queue[32].length = 80;
queue[28].length = 480;
queue[31].length = 140;
for (int i = 0; i < 33; i++) {
if (i == 3 || i == 12)
queue[i].capacity = 40;
else
queue[i].capacity = queue[i].length/10;
queue[i].position = new int[queue[i].capacity];
queue[i].roadqueue = new int[queue[i].capacity];
}
}
然而,當我運行該項目,並插入斷點時, for循環迭代開始,但Netbeans的通知我,例如在
queue[0].length = 6000;
「長度引用空對象」,並在可變Explorer在隊列[]中的所有值在整個程序中保持零。任何想法我做錯了什麼?
請出示一個*短* *,但完整的*程序演示該問題。而不是所有單獨的片段,只顯示一個完整的程序 - 它只需要大約10行... –
您需要在陣列的每個插槽中創建一個隊列對象。 I.e'queue [0] = new Queue();隊列[0] .length = 6000;'等 –
爲什麼變量是靜態的?這樣,每個對象都具有相同的值。我不認爲這是正確的輸出 –