【发布时间】:2015-02-02 08:23:29
【问题描述】:
真的需要帮助,因为病人没有被设置为替换空值。我们必须创建一个包含 50 个空值的数组列表,以便迭代器遍历该列表,如果它找到一个空值,它将把它设置给患者。问题是没有患者被设置为空值。最后我们也要返回床号。
protected int amountOfBeds = 50;
ArrayList<Patient> bedList = new ArrayList<Patient>(amountOfBeds);
public int admitPatient(Patient illPatient) {
int index = -1;
if(illPatient.getAge() > 0 && amountOfBeds > size()) {
//if it is null then set to patient
//if it not null then we assume its a patient so we skip
Iterator<Patient> itr = bedList.iterator();
try{
while(itr.hasNext()) {
int bedIndex = bedList.indexOf(itr.next());
if(bedList.get(bedIndex).equals(null)) {
bedList.set(bedIndex, illPatient);
index = bedIndex +1;
break;
}
}
}catch(NullPointerException e) {
e.getMessage();
}
}
return index;
}
【问题讨论】:
-
为了等于null,不要使用
equals(null),而是使用== null -
您为什么要这样做,而不是简单地让您的列表随着您添加患者并使用
bedList.add(illPatient)而增加? -
谢谢 geet3 那是什么问题!非常感谢!
标签: java arraylist indexing iterator listiterator