如何判断托盘货架行、位、层坐标是否占用?并自动选空货位
托盘货架空货位自动迭代实现方案
现有代码的问题
你当前的实现只是简单递增行、位、层坐标,遇到已占用的坐标时直接放弃添加,但不会主动迭代寻找下一个空货位,无法满足“自动选空货位”的需求。另外,分散的递增逻辑没有明确的遍历优先级,容易出现遍历顺序混乱的情况。
改进思路
核心是实现循环遍历逻辑:从当前记录的lastRow/lastPosition/lastLevel开始,按照预定的货架遍历顺序(比如先遍历层,层满则进位,位满则换行,可根据实际货架布局调整)逐个生成坐标,每生成一个就检查是否已被占用,直到找到空货位为止。找到后更新last系列指针,并将该坐标加入已占用集合。
改进后的代码示例
// 初始化起始坐标,从上次记录的位置开始 int currentRow = main.lastRow; int currentPosition = main.lastPosition; int currentLevel = main.lastLevel; String coordinates = null; boolean foundEmpty = false; // 循环查找空货位,最多遍历所有货位一次(避免死循环) int totalSlots = main.numRows * main.numPositions * main.numLevels; int attempts = 0; while (!foundEmpty && attempts < totalSlots) { // 递增坐标,按 层→位→行 的顺序遍历(可根据实际需求调整) currentLevel++; if (currentLevel >= main.numLevels) { currentLevel = 0; currentPosition++; if (currentPosition >= main.numPositions) { currentPosition = 0; currentRow++; if (currentRow >= main.numRows) { currentRow = 0; } } } // 生成坐标字符串 coordinates = currentRow + "-" + currentPosition + "-" + currentLevel; // 检查是否为空货位 if (!main.assignedCoordinates.contains(coordinates)) { foundEmpty = true; // 更新last指针为当前找到的位置,下次从这里继续 main.lastRow = currentRow; main.lastPosition = currentPosition; main.lastLevel = currentLevel; // 将坐标加入已占用集合 main.assignedCoordinates.add(coordinates); } attempts++; } // 可选:处理所有货位都被占用的情况 if (!foundEmpty) { // 这里可以添加无空货位的异常处理或提示逻辑 System.out.println("所有货位已被占用"); }
关键说明
- 遍历顺序:示例中采用
层→位→行的顺序,你可以根据实际货架的存取逻辑调整(比如行→位→层),只需要修改坐标递增的嵌套顺序即可。 - 死循环防护:通过
totalSlots限制最大尝试次数,避免在所有货位都被占用时陷入无限循环。 - 状态更新:找到空货位后立即更新
last系列指针,确保下次查找从当前位置继续,避免重复遍历已检查过的货位。
内容的提问来源于stack exchange,提问作者Jahidul Hasan Razib
相关产品推荐
相关产品推荐

