2d阵列。查找邻居值的总和

时间:2013-09-10 08:14:33

标签: java arrays sum type-2-dimension

我很难找到一种在二维数组中找到相邻值之和的好方法。我需要在索引位置找到所有相邻值的总和。如果位置是边缘,我需要将它的值转换为总和。因此,每个元素都有4个值对其总和有贡献。

实施例。

  

索引[0] [0] = 5.0的元素。 [0] [1] = 2,[1] [0] = 4.总和   index [0] [0] = 5(左/镜子本身)+5(向上/自身镜像)+   2(右边)+4(低于它)= 16。

到目前为止,我的代码如下。建议更好的方式将不胜感激。此代码正在获取

  

ArrayOutOfBounds:3

在评论后的行上说// ERROR **

Public static double [][] getSumWeight(double [][] map){
    int base = 0;
    //int count = getEnhancementLevel();
    int row = map.length;
    int col = map[0].length;

    int newRow = row*2;
    int newCol = col*2;

    double [] [] sumMap = new double [row][col];
    double [][] weightMap = new double [newRow][newCol];
    //getting the corner sum weights
        for (int i = 0; i < map.length; i++){
            for (int j = 0; j < map[i].length; j++){
                if (i == 0 && j == 0){
                    double result = 0;
                    result += (map[0][0])*2; 
                    result += map[0][1];
                    result += map[1][0];
                    sumMap[0][0] = result;
                    System.out.println("corner" + sumMap[0][0]);
                }
                else if (i == 0 && j == map[0].length){
                    double result = 0;
                    result += map[0][map[0].length];
                    result += map[0][map[0].length-1];
                    result += map[1][map[0].length];
                    sumMap[i][j] = result;

                }else if (i == map.length && j == 0){

                }else if (i == map.length && j == map[map.length].length){

                }
                //getting the mid(s) of the top row
                else if (i == 0 && j != 0 && j != map[0].length){
                    double result = 0;
                    result += map[i][j]; //top value or mirror
                    result += map[i][j-1]; // left value

                    //ERROR****
                    result += map[i][j+1]; // right value
                    result += map[i+1][j]; // bottom value
                    sumMap[i][j] = result;

                }
                //getting the mid(s) of the bottom row
                else if (i == map.length && j != 0 && j != map[map.length].length){
                    double result = 0;
                    result += map[i][j];
                    result += map[i][j-1];
                    result += map[i][j+1];
                    result += map[i-1][j];
                    sumMap[i][j] = result;
                }
                //getting the mid(s) of the left most column
                else if (j == 0 && i != 0 && i != map.length){
                    double result = 0;
                    result += map[i][j];
                    result += map[i-1][j];
                    result += map[i+1][j];
                    result += map[i][j+1];
                    sumMap[i][j] = result;
                }
                //getting the mid(s) of the right most column
                else if (j == map[0].length && i != 0 && i != map.length){
                    double result = 0;
                    result += map[i][j];
                    result += map[i-1][j];
                    result += map[i+1][j];
                    result += map[i][j-1];
                    sumMap[i][j] = result;
                }
                //filling in all the remaining values
                else{
                    double result = 0;
                    result += map[i-1][j];
                    result += map[i+1][j];
                    result += map[i][j-1];
                    result += map[i][j+1];
                    sumMap[i][j] = result;
                }
        }



}
        for (int i = 0; i < map.length; i++){
            for (int j = 0; j < map[i].length; j++){
                System.out.println(sumMap[i][j]);
        }}
        return sumMap;
}

5 个答案:

答案 0 :(得分:1)

 **//The function receives a matrix and replaces each value in the matrix with the value of the sum of its neighbors 
    public static int[][] sumOfNeighbours(int[][] mat) {
        int[][] sum = new int[mat.length][mat[0].length];
        for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat[i].length; j++) {
                if (i == 0) {
                    if (j == 0) {
                        sum[i][j] = mat[i + 1][j + 1] + mat[i][j + 1] + mat[i + 1][j];//dells with the top left corner
                    } else if (j == mat[i].length - 1) {
                        sum[i][j] = mat[i + 1][j - 1] + mat[i][j - 1] + mat[i + 1][j];//dells with the top right corner
                    } else {
                        sum[i][j] = mat[i][j - 1] + mat[i][j + 1] + mat[i + 1][j + 1] + mat[i + 1][j - 1] + mat[i + 1][j];//top middles
                    }
                } else if (i == mat.length - 1) {
                    if (j == 0) {
                        sum[i][j] = mat[i - 1][j] + mat[i - 1][j + 1] + mat[i][j + 1];//dells with the bottom left corner
                    } else if (j == mat[i].length - 1) {
                        sum[i][j] = mat[i - 1][j] + mat[i][j - 1] + mat[i - 1][j - 1];//dells with the bottom right corner
                    } else {
                        sum[i][j] = mat[i - 1][j] + mat[i - 1][j - 1] + mat[i - 1][j + 1] + mat[i][j + 1] + mat[i][j - 1];//dells with the bottom middles
                    }
                } else if (j == 0) {//first column
                    if ((i != 0) && (i != mat.length - 1))
                        sum[i][j] = mat[i - 1][j] + mat[i + 1][j] + mat[i + 1][j + 1] + mat[i - 1][j + 1] + mat[i][j + 1];//dells with the left middles
                } else if (j == mat[i].length - 1) {//last column
                    if ((i != 0) && (i != mat.length - 1))
                        sum[i][j] = mat[i - 1][j] + mat[i + 1][j] + mat[i - 1][j - 1] + mat[i][j - 1] + mat[i + 1][j - 1];//dells with the right  middles
                } else {
                    sum[i][j] = mat[i][j - 1] + mat[i][j + 1] + mat[i + 1][j] + mat[i + 1][j - 1] + mat[i + 1][j + 1] + mat[i - 1][j] + mat[i - 1][j - 1] + mat[i - 1][j + 1];//dells with the all the rest
                }
            }

        }

        return sum;
    }**

答案 1 :(得分:0)

你在行中得到一个ArrayOutOfBounds异常:

result += map[i][j+1]; // right value

因为你到达for循环中的最后一个位置map[i].length并访问下一个(越界)位置map[i][j+1];

请注意,您正在检查您是否错误地到达了最后一个位置:

Example: map[0].length = 4 (0,1,2,3)

else if (i == 0 && j != 0 && j != map[0].length){ // j=3 but j!=4
     result += map[i][j+1] //<- error
}

你应该检查:

else if (i == 0 && j != 0 && j != (map[0].length -1)){

答案 2 :(得分:0)

这是您的解决方案。我知道有很多if else陈述,但这是我最好的。

for(int i=0;i<a.length;i++){
        for(int j=0;j<a[i].length;j++){
            if(i==0){
                if(j==0){
                    sum[i][j] = a[i][j]*2 + a[i+1][j] + a[i][j+1];
                }
                else if(j==a[i].length-1){
                    sum[i][j] = a[i][j]*2 + a[i][j-1] + a[i+1][j] ;
                }
                else{
                    sum[i][j] = a[i][j] + a[i][j-1] + a[i+1][j] + a[i][j+1];
                }
            }
            else if(i==a.length-1){
                if(j==0){
                    sum[i][j] = a[i][j]*2 + a[i-1][j] + a[i][j+1];
                }
                else if(j==a[i].length-1){
                    sum[i][j] = a[i][j]*2 + a[i][j-1] + a[i-1][j] ;
                }
                else{
                    sum[i][j] = a[i][j] + a[i][j-1] + a[i-1][j] + a[i][j+1];
                }
            }
            else if(j==0){
                sum[i][j] = a[i][j] + a[i-1][j] + a[i+1][j] + a[i][j+1];
            }
            else if(j==a[i].length-1){
                sum[i][j] = a[i][j] + a[i-1][j] + a[i+1][j] + a[i][j-1];
            }
            else{
                sum[i][j] = a[i][j-1] + a[i-1][j] + a[i+1][j] + a[i][j+1];
            }
        }
    }

答案 3 :(得分:0)

算法更加简单(我改变了第一个解决方案,因为我理解要求乍一看错误了要求);所有的逻辑都在第一个。我主要摆脱了所有不必要的循环......

    public class MatrixAlgoritm {


        public static double[][] getSumWeight(double[][] map) {
            int row = map.length;
            int col = map[0].length;

            double[][] sumMap = new double[row][col];

            for (int i = 0; i < map.length; i++) {
                for (int j = 0; j < map[i].length; j++) {
                    double result = 0;
                    result += map[Math.max(0, i - 1)][j];
                    if (i == map.length - 1) {
                        result += map[i][j];
                    } else {
                        result += map[i + 1][j];
                    }
                    result += map[i][Math.max(0, j - 1)];
                    if (j == map[0].length - 1) {
                        result += map[i][j];
                    } else {
                        result += map[i][j + 1];
                    }
                    sumMap[i][j] = result;
                }        
            }
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    System.out.println(sumMap[i][j]);
                }
            }
            return sumMap;
        }


        public static void main(String[] args) {
            double[][] baseMap = {{2, 10, 7}, {4, 5, 8}, {5, 6, 9}};
            getSumWeight(baseMap);
        }
    }

答案 4 :(得分:0)

算法非常简单

int sum[][] = new int[c][c];
for(int i = 0; i < arr.length; i++)
{
        for(int j = 0; j < arr[i].length; j++)
            {
                int result = 0;

                if(j > 0)
                    result += arr[i][j-1];
                if(j < arr[i].length - 1)
                    result += arr[i][j+1];
                if(i > 0)
                    result += arr[i-1][j];

                if(i > 0 && j > 0)
                    result += arr[i-1][j-1];
                if(i > 0 && j < arr[i].length - 1)
                    result += arr[i-1][j+1];

                if(j > 0 && i < arr.length -1)
                    result += arr[i+1][j-1];
                if(j < arr[i].length - 1 && i < arr.length -1)
                    result += arr[i+1][j+1];
                if(i < arr.length -1)
                    result += arr[i+1][j];
                sum[i][j] = result;
            }
        }