我已经用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;'等 –
为什么变量是静态的?这样,每个对象都具有相同的值。我不认为这是正确的输出 –