gpt4 book ai didi

java - 队列不删除元素(使用链表)[java]

转载 作者:太空宇宙 更新时间:2023-11-04 14:03:02 26 4
gpt4 key购买 nike

我试图通过制作一个模拟结账队列的非常简单的程序来掌握队列的窍门。由于某种原因,我的自定义出队函数(使用链接列表的removeFirst())实际上不会删除队列中的内容。

这是 CustomerQueue 类

public class CustomerQueue<E> {   

private LinkedList<E> list = new LinkedList<>();
public void enqueue(E e) { list.addLast(e); }
public E dequeue() { return list.removeFirst(); }
public int size() { return list.size(); }
public E element() { return list.getFirst(); }

@Override
public String toString() { return "Queue: " + list.toString(); }

}

这是我的主要内容:

public static void main(String[] args) {

CustomerQueue<Customer> queue = new CustomerQueue<>();

final int totalTime = 720;
int totalServiced = 0;
int totalQueued = 0;
int totalArrived = 1;
int maxQueuedAtOnce = 0;
Customer next = new Customer();

queue.enqueue(next);
System.out.println(next.getATime() + " <-- ATIME STIME --> " + next.getSTime());


for (int minute = 0; minute < totalTime; minute++) {
System.out.println("-- Minute " + (minute + 1) + " ---------");

if (queue.element().getSTime() == 0) {
totalServiced++;
System.out.println("Current customer (" + queue.dequeue() + ") finished being serviced. (Removed)");
//queue.dequeue();
}

if (next.getATime() == 0) {
System.out.println("Customer ID=" + next.getID() + " has arrived.");
queue.enqueue(next);
System.out.println("Customer ID=" + next.getID() + " in queue.");
next = new Customer();
System.out.println("New Customer generated. ID=" + next.getID() + " ATIME: " + next.getATime() + " STIME: " + next.getSTime());
totalArrived++;
totalQueued++;
}
System.out.println("Customer ID=" + next.getID() + " arrival ticked down.");
next.tickArrivalDown();
queue.element().tickServiceDown();
System.out.println("Current queue customer ID=" + next.getID() + " ticked down.");

if (queue.size() > maxQueuedAtOnce)
maxQueuedAtOnce = queue.size();
}

System.out.println("Total Customers Generated " + next.getNumCustomers());
System.out.println("Total Customers Serviced: " + totalServiced);
System.out.println("Total Customers Arrived: " + totalArrived);
System.out.println("Maximum Customers Queued: " + maxQueuedAtOnce);
System.out.println(queue.element());
}

当然还有客户类别:

public class Customer {
private int serviceTime;
private int arrivalTime;
private static int numCustomers = 0;
private int ID;

public int getSTime() { return serviceTime; }
public void setSTime(int t) { serviceTime = t; }
public int getATime() { return arrivalTime; }
public void setATime(int t) { arrivalTime = t; }
public int getID() { return ID; }
public static int getNumCustomers() { return numCustomers; }

Customer() {
serviceTime = (int) (Math.random()*3 + 1);
arrivalTime = (int) (Math.random()*3 + 1);
ID = ++numCustomers;
}

public void tickServiceDown() { serviceTime--; }
public void tickServiceUp() { serviceTime++; }

public void tickArrivalDown() { arrivalTime--; }
public void tickArrivalUp() { arrivalTime++; }

@Override
public String toString() {
return "ID: " + ID + " ArrivalTime: " + arrivalTime + " ServiceTime: " + serviceTime;

}
}

当实例化客户时,我设置的客户类会生成自己的到达时间和完成时间。基本上在循环结束时,下一个客户的到达时间会减少,当前客户的服务时间也会减少。当当前客户的服务时间达到 0 时,应将 LinkedList 中的第一个元素出队/删除。由于某种原因它没有删除它。这是循环后的输出:

Total Customers Generated 358
Total Customers Serviced: 1
Total Customers Arrived: 358
Maximum Customers Queued: 357
ID: 1 ArrivalTime: 0 ServiceTime: -717

我完全被难住了,无论用多少谷歌搜索都没有帮助。

最佳答案

查看你的输出

ID: 1 ArrivalTime: 0 ServiceTime: -717

ServiceTime 是 getSTime()

返回的内容

然后作为 0 != -717 我认为下面的内容就可以了

How about if (queue.element().getSTime() <= 0) {

关于java - 队列不删除元素(使用链表)[java],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29136312/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com