You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java疫苗接种亭管理系统remove方法及等待队列分配bug求解

疫苗接种亭管理系统remove方法修复方案

问题定位

两个问题本质都是remove方法的分配逻辑存在范围限制和空槽补位逻辑缺失:

  • 原方法仅对单个接种亭的队列做分配判断,没有遍历全部6个接种亭做状态检查
  • 移除患者后没有立刻触发等待队列到空接种位的调度逻辑

修复思路

  1. 移除患者后先定位到空出的接种亭索引,优先从对应等待队列取队首患者补位
  2. 补全全量分配逻辑,遍历所有接种亭的空位,从对应专属等待队列批量分配患者,覆盖全部6个接种亭
  3. 补充等待队列元素前移逻辑,保证队列顺序正确

修复后代码示例

public static void remove(String booth[], String waiting[][]) {
    // 定位2号接种亭(数组从0开始计数,对应索引为1),清空被移除患者的位置
    int targetBoothIndex = 1;
    booth[targetBoothIndex] = null;

    // 第一部分:处理2号亭移除后的自动补位逻辑
    for (int i = 0; i < waiting[targetBoothIndex].length; i++) {
        if (waiting[targetBoothIndex][i] != null) {
            // 将等待队列首位患者分配到空出的接种位
            booth[targetBoothIndex] = waiting[targetBoothIndex][i];
            // 等待队列剩余元素整体前移一位
            for (int j = i; j < waiting[targetBoothIndex].length - 1; j++) {
                waiting[targetBoothIndex][j] = waiting[targetBoothIndex][j + 1];
            }
            waiting[targetBoothIndex][waiting[targetBoothIndex].length - 1] = null;
            break;
        }
    }

    // 第二部分:全量分配逻辑,实现向全部6个接种亭分配等待患者
    for (int boothIdx = 0; boothIdx < booth.length; boothIdx++) {
        // 跳过已有患者的接种亭
        if (booth[boothIdx] != null) {
            continue;
        }
        // 遍历当前接种亭对应的等待队列,取首位可用患者
        for (int k = 0; k < waiting[boothIdx].length; k++) {
            if (waiting[boothIdx][k] != null) {
                booth[boothIdx] = waiting[boothIdx][k];
                // 等待队列元素前移
                for (int m = k; m < waiting[boothIdx].length - 1; m++) {
                    waiting[boothIdx][m] = waiting[boothIdx][m + 1];
                }
                waiting[boothIdx][waiting[boothIdx].length - 1] = null;
                break;
            }
        }
    }
}

逻辑适配说明

  • 代码默认接种亭数组booth长度为6,索引0对应1号接种亭,索引1对应2号接种亭,以此类推
  • 二维等待数组waiting第一维下标与接种亭索引一一对应,每个下标对应一个接种亭的专属等待队列
  • 如果业务逻辑为所有接种亭共享全局等待队列,只需将分配逻辑修改为遍历全局队列取首个患者即可,无需按接种亭索引匹配等待队列

内容的提问来源于stack exchange,提问作者YOMAN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 04:48:02