队列没有正确添加对象

时间:2017-04-01 22:43:18

标签: java queue

我遇到了一些问题,例如在标题中,将对象添加到java中的队列中。我正在尝试编写一些模拟收费站,汽车和队列的代码。以下是代码。

    //Below is creating and naming objects and variables
    int numberOfCars, numberOfTollbooths; 
    double tollboothTotalServiceTime = 0, tollboothAverageServiceTime = 0;
    Scanner keyboard = new Scanner(System.in);
    ExpRandom random = new ExpRandom(13);
    Queue<Car> carQueue = new LinkedList<Car>();

    //Asks the user how many cars need to be created for this simulation
    System.out.print("How many cars do you want to simulate? ");
    numberOfCars = Integer.parseInt(keyboard.next());

    for(int i = 1; i <= numberOfCars; i++)
    {
        carQueue.add(new Car(i, Increasing.increasingNumber()/10));
        System.out.println("Car queue: [" + carQueue.poll() + "]");
    }

输出:

#1 arrived at time 1.
Car queue: [#1]
#2 arrived at time 2.
Car queue: [#2]
#3 arrived at time 4.
Car queue: [#3]
#4 arrived at time 18.
Car queue: [#4]
#5 arrived at time 33.
Car queue: [#5]
#6 arrived at time 38.
Car queue: [#6]
#7 arrived at time 42.
Car queue: [#7]
#8 arrived at time 47.
Car queue: [#8]
#9 arrived at time 48.
Car queue: [#9]
#10 arrived at time 52.
Car queue: [#10]
#11 arrived at time 53.
Car queue: [#11]
#12 arrived at time 58.
Car queue: [#12]

一旦添加了所有车辆,我怎么能看到整个汽车车厢?像汽车队列的东西:[1,2,3,4 ...]

1 个答案:

答案 0 :(得分:0)

队列的

poll removes the head,请改用peek

System.out.println("Car queue: [" + carQueue.peek() + "]");