m0_65749592 2024-06-12 16:50 采纳率: 35.5%
浏览 2

规律矩阵多行6列,getMatrixList

    public void updateMatrix(List<String> bomb, List<List<Integer>> getMatrixList) {
    for (String bombItem : bomb) {
        String[] bombParts = bombItem.split("_");
        if (bombParts.length == 3) {
            int row = Integer.parseInt(bombParts[0]);
            int column = Integer.parseInt(bombParts[1]);
            int value = Integer.parseInt(bombParts[2]);
            
            int chunkSize = 5; // 大小为5
            int chunkIndex = column / chunkSize; 
            
           
            int startRow = chunkIndex * chunkSize;
            int endRow = startRow + chunkSize - 1;
            
            for (int i = Math.max(0, startRow); i <= Math.min(endRow, getMatrixList.size() - 1); i++) {
                if (value == 1000) {
                    if (column >= startRow && column <= endRow) {
                        getMatrixList.get(i).set(column % chunkSize, 5);
                    }
                } else if (value >= 10 && value <= 25) {
                    if (column >= startRow && column <= endRow) {
                        getMatrixList.get(i).set(column % chunkSize, 6);
                    }
                } else if (value >= 50 && value <= 100) {
                    if (column >= startRow && column <= endRow) {
                        getMatrixList.get(i).set(column % chunkSize, 7);
                    }
                }
            }
        }
    }
}
 需求: 这个代码要做改动,如果 bomb=[0_2_10,1_3_4,2_10_10,3_12_25,4_34]行_列_值,我现在代码是拿到值就把位置改了,
我现在希望如果列的位置是>=0 &&<=5,注意是列不是行,就去getMatrixList第1行到第5行里面改变矩阵值,
 如果列是  >=6&& <=11 那就去getMatrixList第6行到第10行里面开始改变矩阵值,如果列是  >=12 &&<=17 那就去getMatrixList第11行开始到第15行改变矩阵值
 如果列是 >=18 &&<=23 那就去getMatrixList第16行开始到第20行改变矩阵值,如果列是 >=24 &&<=29那就去getMatrixList第21行开始到第25行改变矩阵值
 如果列是 >=30&& <=35那就去getMatrixList第26行开始到第30行改变矩阵值  , 如果列是 >=36&& <=41那就去getMatrixList第31行开始到第35行改变矩阵值   
能看懂这规律吗帮我改一下代码  中文解释
  • 写回答

1条回答 默认 最新

  • 阿里嘎多学长 2024-06-12 17:03
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    根据你的需求,我们需要对updateMatrix方法进行一些调整,以适应新的更新逻辑。下面是修改后的Java代码:

    public void updateMatrix(List<String> bomb, List<List<Integer>> getMatrixList) {
        for (String bombItem : bomb) {
            String[] bombParts = bombItem.split("_");
            if (bombParts.length == 3) {
                int row = Integer.parseInt(bombParts[0]);
                int column = Integer.parseInt(bombParts[1]);
                int value = Integer.parseInt(bombParts[2]);
    
                int chunkSize = 6; // 每块6列
    
                // 计算列所在的块的起始行
                int startRow = (column / chunkSize) * 5 + 1;
    
                // 根据列的值确定更新的行数范围
                int endRow = startRow + 4; // 每块5行
    
                // 确保行数不会超出矩阵的边界
                endRow = Math.min(endRow, getMatrixList.size());
    
                for (int i = startRow; i <= endRow; i++) {
                    // 根据value的值更新矩阵中的相应位置
                    int matrixColumn = column % chunkSize;
                    if (value == 1000) {
                        getMatrixList.get(i).set(matrixColumn, 5);
                    } else if (value >= 10 && value <= 25) {
                        getMatrixList.get(i).set(matrixColumn, 6);
                    } else if (value >= 50 && value <= 100) {
                        getMatrixList.get(i).set(matrixColumn, 7);
                    }
                }
            }
        }
    }
    

    这段代码的主要变化是:

    1. chunkSize设置为6,因为我们每块有6列。
    2. 计算startRow时,我们使用(column / chunkSize) * 5 + 1来确定列所在的块的起始行。
    3. endRow计算为startRow + 4,因为我们每块有5行。
    4. 使用Math.min确保endRow不会超出矩阵的边界。
    5. 循环从startRow开始到endRow结束,更新矩阵中的相应列。

    请注意,这段代码假设getMatrixList的大小至少有35行,因为根据你的描述,最后一个块的行数是第31行到第35行。如果getMatrixList的大小小于35行,你需要在代码中添加额外的逻辑来处理这种情况。

    至于参考资料,你可以查看Java的官方文档,特别是关于ListArrayList的部分,以及如何处理数组和循环的基础知识。Java的官方文档可以在Oracle的网站上找到,地址是:Java SE Documentation

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 6月12日

悬赏问题

  • ¥30 模拟电路 logisim
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价