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行改变矩阵值
能看懂这规律吗帮我改一下代码 中文解释
规律矩阵多行6列,getMatrixList
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
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); } } } } }这段代码的主要变化是:
- 将
chunkSize设置为6,因为我们每块有6列。 - 计算
startRow时,我们使用(column / chunkSize) * 5 + 1来确定列所在的块的起始行。 endRow计算为startRow + 4,因为我们每块有5行。- 使用
Math.min确保endRow不会超出矩阵的边界。 - 循环从
startRow开始到endRow结束,更新矩阵中的相应列。
请注意,这段代码假设
getMatrixList的大小至少有35行,因为根据你的描述,最后一个块的行数是第31行到第35行。如果getMatrixList的大小小于35行,你需要在代码中添加额外的逻辑来处理这种情况。至于参考资料,你可以查看Java的官方文档,特别是关于
List和ArrayList的部分,以及如何处理数组和循环的基础知识。Java的官方文档可以在Oracle的网站上找到,地址是:Java SE Documentation。解决 无用评论 打赏 举报 编辑记录- 将