java - 重写方法以从索引中删除项目并将索引中所有剩余项目数减去 1

标签 java arrays jakarta-ee java-ee-6 java-ee-5

我需要重写方法以从索引中删除项目并将索引中所有剩余项目的数量减去 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/

相关文章:

java - 如何在循环中列出多维数组值

c - 使用字符串读取数字

java - 如何从 Java 中的文件创建 ByteArrayInputStream?

java - 如何从 servlet 获取 EJB 项目中客户端的远程 IP 地址?

java - 在 2 个表之间拆分 Hibernate 实体

java - 无法在浏览器中运行Java Applet

java - 如何检查 Handler 是否有 Activity 任务?

java - 从 SugarORM 获取第一个索引

php - 如何通过 MySQL 表将 Python 列表转换为 PHP 数组

java - 如何确定 MDB 的 messageType 接口(interface)