m0_65749592 2024-06-11 18:47 采纳率: 35.5%
浏览 3
已结题

getMatrixList里面可能存储很多数据,5行算是一个矩阵

    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 originalColumns = getMatrixList.isEmpty() ? 0 : getMatrixList.get(0).size();

            for (int i = getMatrixList.size(); i <= row; i++) {
                List<Integer> newRow = new ArrayList<>();
                for (int j = 0; j < originalColumns; j++) {
                    newRow.add(0); // 初始化为0
                }
                getMatrixList.add(newRow);
            }
            if (value == 1000) {
                if (row < getMatrixList.size()) {
                    if (column < getMatrixList.get(row).size()) {
                        getMatrixList.get(row).set(column, 5);
                    }
                }
            } else if (value >= 10 && value <= 25) {
                if (row < getMatrixList.size()) {
                    if (column < getMatrixList.get(row).size()) {
                        getMatrixList.get(row).set(column, 6);
                    }
                }
            } else if (value >= 50 && value <= 100) {
                if (row < getMatrixList.size()) {
                    if (column < getMatrixList.get(row).size()) {
                        getMatrixList.get(row).set(column, 7);
                    }
                }
            }
        }
    }
}
// 需求:  帮我解决一下这个问题, 假设:gameData.getBingoList()=[0_1_2,3_9_2,7_5_2,7_14_2,5_10_2,1_2_2,3_18_2,0_14_2,2_20_2](下标行_列_翻倍值),
getMatrixList里面是存储了很多行数据 ,每5行是相当于一个矩阵,注意getBingoList是下标存储,从0开始的,因为这是一个56列的矩阵,我希望getMatrixList里面的值去改变矩阵的值,
举例一个getBingoList里面的3_9_2 其实意思就是: 第2个矩阵的4行第4列 能理解我意思吗 所以我有时候set有问题getMatrixList.get(row).set(column, 5); 没有找到具体位置  怎么改
  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-06-11 18:48
    关注

    引自免费微信小程序:皆我百晓生

    为了更准确地处理getBingoList中的信息并正确更新getMatrixList中的矩阵,我们首先需要明确几个关键点:

    1. getBingoList中的元素格式为行_列_翻倍值
    2. getMatrixList包含多个矩阵,每个矩阵有5行。
    3. 矩阵的列数固定为6列。

    基于这些信息,我们可以调整updateMatrix方法以确保正确的矩阵和位置被更新。以下是一个修改后的示例代码:

    public void updateMatrix(List<String> bingoList, List<List<Integer>> getMatrixList) {
        // 矩阵的列数
        final int columns = 6;
    
        for (String bingoItem : bingoList) {
            String[] parts = bingoItem.split("_");
            if (parts.length == 3) {
                int matrixIndex = Integer.parseInt(parts[0]) / 5; // 确定是哪个矩阵
                int rowInMatrix = Integer.parseInt(parts[0]) % 5; // 确定在矩阵中的行
                int column = Integer.parseInt(parts[1]); // 列位置
                int value = Integer.parseInt(parts[2]); // 翻倍值
    
                // 计算并确保矩阵存在
                while (getMatrixList.size() <= matrixIndex) {
                    List<Integer> newRow = new ArrayList<>(Collections.nCopies(columns, 0));
                    getMatrixList.add(newRow);
                }
    
                // 确保矩阵行数足够
                while (getMatrixList.get(matrixIndex).size() <= rowInMatrix) {
                    getMatrixList.get(matrixIndex).add(0);
                }
    
                // 更新矩阵值
                if (value == 1000) {
                    getMatrixList.get(matrixIndex).set(rowInMatrix * columns + column, 5);
                } else if (value >= 10 && value <= 25) {
                    getMatrixList.get(matrixIndex).set(rowInMatrix * columns + column, 6);
                } else if (value >= 50 && value <= 100) {
                    getMatrixList.get(matrixIndex).set(rowInMatrix * columns + column, 7);
                }
            }
        }
    }
    

    但是,这里有一个细节需要注意:上述代码中的set方法调用方式不正确,因为它假设了列表是扁平的,而实际上我们需要的是二维结构。因此,正确的set方法调用应该如下所示:

    // 更新矩阵值
    if (value == 1000) {
        getMatrixList.get(matrixIndex).get(rowInMatrix).set(column, 5);
    } else if (value >= 10 && value <= 25) {
        getMatrixList.get(matrixIndex).get(rowInMatrix).set(column, 6);
    } else if (value >= 50 && value <= 100) {
        getMatrixList.get(matrixIndex).get(rowInMatrix).set(column, 7);
    }
    

    确保在添加新行时,也遵循5行的矩阵结构:

    while (getMatrixList.get(matrixIndex).size() <= rowInMatrix) {
        getMatrixList.get(matrixIndex).add(new ArrayList<>(Collections.nCopies(columns, 0)));
    }
    

    这样,每次添加新行时,都会创建一个新的列表(即矩阵的一行),并将其添加到当前矩阵中。这将确保getMatrixList中的每个矩阵都保持正确的结构。

    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月12日
  • 创建了问题 6月11日

悬赏问题

  • ¥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 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错