如何使用for循环将不同的对象添加到队列

时间:2019-09-11 09:56:32

标签: java queue

我想将10个类型为CustomerQueue的记录添加到位于CustomerQueue本身内的CustomerQueue数组中,以打印该数组。问题在于,它一直“排队”到位置0,因为对于每个CustomerQueue对象,后部都重置为0。对此有任何解决方法吗?

主要:

CustomerQueue cQ = null;
    for (int i = 0; i < 10; i++) {
        cQ = new CustomerQueue(1, 0, false);
        cQ.enqueue(cQ);
        System.out.println(cQ.arrivalTime);
    }
System.out.print("\n");

    System.out.println(Arrays.toString(cQ.array));

构造函数:

public CustomerQueue(double aT, double tT, boolean pM) {
    aT = (double) (Math.random() * (100 - 1 + 1)) + 1;
    this.arrivalTime = aT;
    this.tallyTime = tT;
    this.paymentMethod = pM;
    capacity = 500;
    front = 0;
    rear = -1;
    count = 0;
}

入队:

public void enqueue(CustomerQueue cQ) {
    if (isFull()) {
        System.out.println("OverFlow\nProgram Terminated");
        System.exit(1);
    }

    rear = (rear + 1);
    array[rear] = cQ;
    count++;
}

1 个答案:

答案 0 :(得分:0)

更改:

CustomerQueue cQ = null;
    for (int i = 0; i < 10; i++) {
        cQ = new CustomerQueue(1, 0, false);
        cQ.enqueue(cQ);
        System.out.println(cQ.arrivalTime);
    }

CustomerQueue cQ = null;
    for (int i = 0; i < 10; i++) {
        CustomerQueue subQueue = new CustomerQueue(1, 0, false);
        cQ.enqueue(subQueue);
        System.out.println(subQueue.arrivalTime);
    }

如果您的要求超出此范围,请发表评论。