我创建了一个 arrayList treat
来显示治疗室的 5 个实例,我还为治疗室创建了一个 linkedList inTreatment
以将 5 个患者对象从queue
,但是当我将多个对象传递给 linkedList 时,它们会不断替换添加的第一个元素,而不是移动到下一个可用元素。我认为问题在于 inTreatment.add
行,但我不确定如何引用下一个可用索引。我们非常欢迎所有建议。
下面是我创建数组并添加到 inTreatment
链接列表的代码。
创建治疗室数组
public static void createTreatmentRooms() {
for (int i = 0; i < treat.length; i++) {
treat[i] = new TreatmentRoom();
treat[i].setAvailable(true);
}
}
添加到治疗室方法
for (int i = 0; i < TreatmentRoom.treat.length; i++) {
if ((TreatmentRoom.treat[i].isAvailable())
&& (Queue.queue.size() != 0)
&& (Queue.queue.getFirst().getTriage() != Status.NOT_ASSESSED)) {
// add patient to inTreatment list for future sorting...
inTreatment.add(queue.getFirst());
System.out.println("taken to treatment queue");
// remove patient from front of queue
for (Patient p : queue) {
System.out.println(p.getFirstName());
}
queue.poll();
System.out.println("second queue");
for (Patient p : queue) {
System.out.println(p.getFirstName());
}
System.out.println("removed from queue");
// if free, add patient to treatment room
TreatmentRoom.treat[i].setPatient(inTreatment.getFirst());
System.out.println("sent to treatment room"
+ TreatmentRoom.treat[i]);
// System.out.println("patient added" +
// queue.get(i).getFirstName());
// set treatment room to unavailable
TreatmentRoom.treat[i].setAvailable(false);
System.out.println("treatment room busy");
} else {
System.out.println("Treatment room is not available");
}
}
}
最佳答案
问题可能出在这里:
queue.remove(i);
您正在删除索引 i
处的元素,但是 i
在房间范围内,与队列无关,是吗?
您可能想要移除第一个元素。
旁注:应该有一个 poll()
方法可以让您顺便在一次调用中查看和删除队列的第一个元素,但我不确定您在这里使用的是什么类型的队列,它看起来不像 java.util.Queue
。
关于java - 将对象传递给链表中的下一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29927646/