简体   繁体   English

我正在尝试编写一种方法,该方法对数组中的相邻单元格求和,并使用获得的值填充第二个数组中的位置

[英]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

I am pretty new to java and am wondering how I would take anxn array with these values and sum the surrounding values and then insert it into secondArray perhaps using a for loop? 我是java的新手,我想知道如何使用这些值的焦数数组并将周围的值相加,然后将它插入secondArray,也许使用for循环? I appreciate the help. 我很感激帮助。

firstArray: firstArray:

1 1 0 1 0 1 1 1 0 1 0 1

1 0 1 0 1 0 1 0 1 0 1 0

1 1 0 0 1 1 1 1 0 0 1 1

1 1 0 0 1 1 1 1 0 0 1 1

0 0 0 1 1 0 0 0 0 1 1 0

secondArray: secondArray:

2 3 3 2 3 1 2 3 3 2 3 1

4 6 3 3 4 4 4 6 3 3 4 4

4 5 3 4 4 4 4 5 3 4 4 4

3 3 3 4 5 4 3 3 3 4 5 4

2 2 2 2 3 3 2 2 2 2 3 3

Here's the mechanics (added one last row to the matrix as you said the matrix is NxN): 这是机制(当你说矩阵是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();
}

Now, you just have to implement the static int sumOfSurrounding( int[][] matrix, int i, int j ) method. 现在,您只需要实现static int sumOfSurrounding( int[][] matrix, int i, int j )方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试将数组方法转换为ArrayList方法 - I am trying to convert an array method to an ArrayList method 相邻数组的最小值 - Min value of adjacent array 检查2D数组中的相邻单元-Java - Check Adjacent Cells in 2D Array - Java 编写一个函数来计算数组中的完美总和? - Write a function which computes the perfect sums in an array? 我正在尝试创建一个方法,将车辆的数组列表转换为数组并返回数组 - I am trying to create a method that converts the arrayList of vehicles to an array and returns the array 如何将数组拆分为 3 个部分,例如 int[] {8,3,6,2,9,5,1,7,4}。 我正在尝试编写一个合并排序方法,但我已经被困了一天多了 - How to split an Array into 3 parts e.g int[] {8,3,6,2,9,5,1,7,4}. i am trying to write a mergesort method and i havve been stuck for over a day 我正在尝试在字符串数组中查找特定字符。 我最终必须使用什么? - I am trying to find the specific character within a String Array. What must I end up using? 我正在尝试将添加到面板的JButton数组添加到JFrame上,但是什么都没有显示 - I am trying to add a JButton array that I added to a panel onto my JFrame, yet nothing is showing up 我试图调用的方法不是返回值 - The method I am trying to call is not returning a value 我正在尝试将 JSON 数组发布到 spring-boot controller 但没有任何反应,甚至没有出现在日志中 - I am trying to POST a JSON array to a spring-boot controller but nothing happens or even shows up in the logs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM