确实需要帮助,因为患者没有设置替换空值。我们必须创建一个包含 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;
}
最佳答案
创建 50 个空列表的简单方法是这样的
List<Patient> list = Collections.nCopies(50, null);
查找 null 索引的快速方法是这样的
int i = list.indexOf(null);
关于java - 创建一个空数组列表,然后将索引设置为一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27294314/