[英]I am trying to write a method that sums up adjacent cells in array and fills the position in a second array with the value obtained
我是java的新手,我想知道如何使用這些值的焦數數組並將周圍的值相加,然后將它插入secondArray,也許使用for循環? 我很感激幫助。
firstArray:
1 1 0 1 0 1
1 0 1 0 1 0
1 1 0 0 1 1
1 1 0 0 1 1
0 0 0 1 1 0
secondArray:
2 3 3 2 3 1
4 6 3 3 4 4
4 5 3 4 4 4
3 3 3 4 5 4
2 2 2 2 3 3
這是機制(當你說矩陣是NxN時,將最后一行添加到矩陣中):
int[][] matrix = new int[][]{
{ 1, 1, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 0 },
{ 1, 1, 0, 0, 1, 1 },
{ 1, 1, 0, 0, 1, 1 },
{ 0, 0, 0, 1, 1, 0 },
{ 0, 0, 0, 0, 0, 0 }
};
int[][] result = new int[ matrix.length ][ matrix.length ];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
result[ i ][ j ] = sumOfSurrounding( matrix, i, j );
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
System.out.print( result[ i ][ j ] + " " );
}
System.out.println();
}
現在,您只需要實現static int sumOfSurrounding( int[][] matrix, int i, int j )
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.