[英]Java - Get all values around a number
我想制作一個可更改值的矩陣/將位置上的數字加上其上,下,左和右的值進行總和。
當前的問題是9 3 9 | 6 12 6 | 9 3 9
9 3 9 | 6 12 6 | 9 3 9
9 3 9 | 6 12 6 | 9 3 9
但應該是9 10 9 | 6 10 6| 9 10 9
9 10 9 | 6 10 6| 9 10 9
9 10 9 | 6 10 6| 9 10 9
(從左到右,從左到右),我不知道我得到上面的值。
有人可以幫我嗎?
此外,我想在一個自動循環中執行此操作,但隨后總是得到此ArrayIndexOutOfBoundsException
。
輸入矩陣為1 8 1 | 4 2 4 | 1 8 1
1 8 1 | 4 2 4 | 1 8 1
1 8 1 | 4 2 4 | 1 8 1
,結果應該是13 12 13 | 8 26 8 | 13 12 13
13 12 13 | 8 26 8 | 13 12 13
public class Matrix { public static void main(String[] args) { int[][] matrix = { { 1, 8, 1 }, { 4, 2, 4 }, { 1, 8, 1 } }; print(matrix); System.out.println("\\n"); int[][] blur = blurMatrix(matrix); print(blur); } public static int[][] blurMatrix(int[][] matrix) { if (matrix == null) return null; if (matrix.length <= 0) return null; int[][] blur = new int[matrix.length][]; for (int row = 0; row < blur.length; row++) { blur[row] = new int[matrix[row].length]; for (int col = 0; col < blur[row].length - 1; col++) { int cellValue = matrix[row][col]; int nextColValue = matrix[row][col + 1]; // int lastColValue = matrix[row][col-1]; // int nextRowValue = matrix[row+1][col]; // int lastRowValue = matrix[row-1][col]; blur[row][col] = cellValue + nextColValue;// +lastColValue+nextRowValue+lastRowValue; } int lastColumnIndex = blur[row].length - 1; blur[row][2] = blur[row][0]; blur[row][1] = matrix[row][2] + matrix[row][0] + matrix[row][2]; } return blur; } public static void print(int[][] m) { if (m == null) { System.out.print(" no matrix to print!"); return; } for (int row = 0; row < m.length; row++) { for (int col = 0; col < m[row].length; col++) { System.out.print(m[row][col] + " "); } System.out.println(""); } } }
您需要一個條件,第三級if-then-else表達式。
int cols = matrix[row].length;
int nextColValue = col+1 < cols ? matrix[row][col+1] : 0;
int lastColValue = col-1 >= 0 ? matrix[row][col-1] : 0;
... + nextColValue + lastColValue + ...
當然可以:
int valueAt(int[][] m, int row, int col) {
if (0 > row || row >= m.length) {
return 0;
}
int[] v = m[row];
if (0 > col || col >= v.length) {
return 0;
}
return v[col];
}
... = valueAt(matrix, row, col)
+ valueAt(matrix, row-1, col)
+ valueAt(matrix, row+1, col)
+ valueAt(matrix, row, col-1)
+ valueAt(matrix, row, col+1);
由此避免了矩陣[0] [-1]引起ArrayIndexOutOfBoundsException的問題。
你必須實現你的
如果沒有元素,則應添加0
因此,請檢查每行或col±1(如果您離開了數組的邊界)
public static int[][] blurMatrix(int[][] matrix){
if(matrix == null || matrix.length <= 0)
return null;
int[][] returnMatrix = new int[matrix.length][matrix[0].length];
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
int topValue = row == 0 ? 0 : matrix[row-1][col];
int bottomValue = row == matrix.length-1 ? 0 : matrix[row+1][col];
int leftValue = col == 0 ? 0 : matrix[row][col-1];
int rightValue = col == matrix[row].length-1 ? 0 : matrix[row][col+1];
int cellValue = matrix[row][col];
int sum = topValue + leftValue + cellValue + rightValue + bottomValue;
returnMatrix[row][col] = sum;
}
}
return returnMatrix;
}
輸入
1 8 1
4 2 4
1 8 1
產量
13 12 13
8 26 8
13 12 13
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.