我需要重写方法以从索引中删除项目并将索引中所有剩余项目的数量减去 1。
包括上述条件,我需要重写“getLot”,这样它就不会依赖索引号来查找批处理。它应该只能根据批号找到批号。因此,如果批号 2 被删除,那么批号 3 将从索引 2 移动到索引 1,并且仍然被视为批号 3,而不是通过索引 2,因为它不再是索引 2。 (顺便说一句,索引从 0 开始)
这是我的代码:
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;
}
}
谢谢。
最佳答案
您可以使用commons lang's ArrayUtils
为:
array = ArrayUtils.removeElement(array, element)
关于java - 重写方法以从索引中删除项目并将索引中所有剩余项目数减去 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15215199/