我需要重写方法以从索引中删除项目并将索引中所有剩余的项目编号减去 1。
包括上述情况,我需要重写“getLot”,使其不依赖索引号来查找批次。它应该只根据他们的批号找到批次。因此,如果删除了批号 2,那么批号 3 将从索引 2 移动到索引 1 并且仍然作为批号 3 找到,而不是通过不再是索引 2 找到。(索引从 0 btw 开始)
这是我的代码:
public Lot getLot(int lotNumber)
{
if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {
// The number seems to be reasonable.
Lot selectedLot = lots.get(lotNumber - 1);
// Include a confidence check to be sure we have the
// right lot.
if(selectedLot.getNumber() != lotNumber) {
System.out.println("Internal error: Lot number " +
selectedLot.getNumber() +
" was returned instead of " +
lotNumber);
// Don't return an invalid lot.
selectedLot = null;
}
return selectedLot;
}
else {
System.out.println("Lot number: " + lotNumber +
" does not exist.");
return null;
}
}
谢谢你。