Java 查找数组中特定元素周围有多少具有特定值的相邻元素的有效方法

Java 查找数组中特定元素周围有多少具有特定值的相邻元素的有效方法,java,arrays,Java,Arrays,如果我有一个名为myArray的2D int数组,例如3x3大小,并且它的元素只能有1或0作为值,那么什么是计算特定元素周围有多少个1的有效方法?例如: [0][0][0] [0][1][0] [1][1][1] 元素myArray[0][0]的neighbourCount为1,而myArray[0][1]的neighbourCount为3 这是我目前的暴力代码。myArray=currentGeneration public int neighbours(int x, int y) { /

如果我有一个名为myArray的2D int数组,例如3x3大小,并且它的元素只能有1或0作为值,那么什么是计算特定元素周围有多少个1的有效方法?例如:

[0][0][0]
[0][1][0]
[1][1][1]
元素myArray[0][0]的neighbourCount为1,而myArray[0][1]的neighbourCount为3

这是我目前的暴力代码。myArray=currentGeneration

public int neighbours(int x, int y)  { //x and y are 0 index based coordinates, they are swapped inside to corespond with actual x and y coordinates
        int neighbourCounter = 0;
        if(x == 0 && y == 0) {
            if(currentGeneration[y+1][x] == 1) {
                neighbourCounter++;
            }
            if(currentGeneration[y][x+1] == 1) {
                neighbourCounter++;
            }
            if(currentGeneration[y+1][x+1] == 1) {
                neighbourCounter++;
            }
        } else if(x == 0 && y == currentGeneration.length - 1) {
                            if(currentGeneration[y-1][x] == 1) {
                                    neighbourCounter++;
                            }
                            if(currentGeneration[y][x+1] == 1) {
                                    neighbourCounter++;
                            }
                            if(currentGeneration[y-1][x+1] == 1) {
                                    neighbourCounter++;
                            }
        } else if(x == currentGeneration[0].length - 1 && y == currentGeneration.length - 1) {
                            if(currentGeneration[y-1][x] == 1) {
                                    neighbourCounter++;
                            }
                            if(currentGeneration[y][x-1] == 1) {
                                    neighbourCounter++;
                            }
                            if(currentGeneration[y-1][x-1] == 1) {
                                    neighbourCounter++;
                            }
        } else if( y == 0 && x == currentGeneration[0].length - 1) {
                            if(currentGeneration[y][x-1] == 1) {
                                    neighbourCounter++;
                            }
                            if(currentGeneration[y+1][x] == 1) {
                                    neighbourCounter++;
                            }
                            if(currentGeneration[y+1][x-1] == 1) {
                                    neighbourCounter++;
                            }
        } else if(y == 0) {
            for(int i = -1; i <= 1; i+=2) {
                if(currentGeneration[y][x+i] == 1) {
                    neighbourCounter++;
                }
            }
            if(currentGeneration[y+1][x] == 1) {
                neighbourCounter++;
            }
            for(int i = -1; i <= 1; i+=2) {
                if(currentGeneration[y+1][x+i] == 1) {
                    neighbourCounter++;
                }
            }
        } else if(x == 0) {
            for(int i = -1; i <= 1; i+=2) {
                if(currentGeneration[y+i][x] == 1) {
                    neighbourCounter++;
                }
            }
            if(currentGeneration[y][x+1] == 1) {
                neighbourCounter++;
            }
            for(int i = -1; i <= 1; i+=2) {
                if(currentGeneration[y+i][x+1] == 1) {
                    neighbourCounter++;
                }
            }
        } else if(y == currentGeneration.length - 1) {
            for(int i = -1; i <= 1; i+=2) {
                if(currentGeneration[y][x+i] == 1) {
                    neighbourCounter++;
                }
            }
            if(currentGeneration[y-1][x] == 1) {
                neighbourCounter++;
            }
            for(int i = -1; i <= 1; i+=2) {
                if(currentGeneration[y-1][x+i] == 1) {
                    neighbourCounter++;
                }
            }
        } else if(x == currentGeneration[0].length - 1) {
            for(int i = -1; i <= 2; i+=2) {
                if(currentGeneration[y+i][x] == 1) {
                    neighbourCounter++;
                }
            }
            if(currentGeneration[y][x-1] == 1) {
                neighbourCounter++;
            }
            for(int i = -1; i <= 2; i+=2) {
                if(currentGeneration[y+i][x-1] == 1) {
                    neighbourCounter++;
                }
            }
        } else {
            for(int i = -1; i <= 1; i+=2) {
                if(currentGeneration[y+i][x] == 1) {
                    neighbourCounter++;
                }
                if(currentGeneration[y][x+i] == 1) {
                    neighbourCounter++;
                }
                if(currentGeneration[y+i][x+i] == 1) {
                    neighbourCounter++;
                }
                if(currentGeneration[y+i][x-i] == 1) {
                    neighbourCounter++;
                }
            }
        }
        return neighbourCounter;
    }
公共int邻域(intx,inty){//x和y是基于索引的0坐标,它们在内部交换以与实际的x和y坐标共同响应
int-neighbourCounter=0;
如果(x==0&&y==0){
如果(当前代[y+1][x]==1){
neighbourCounter++;
}
如果(当前代[y][x+1]==1){
neighbourCounter++;
}
如果(当前代[y+1][x+1]==1){
neighbourCounter++;
}
}else if(x==0&&y==currentGeneration.length-1){
如果(当前代[y-1][x]==1){
neighbourCounter++;
}
如果(当前代[y][x+1]==1){
neighbourCounter++;
}
如果(当前代[y-1][x+1]==1){
neighbourCounter++;
}
}else if(x==currentGeneration[0]。长度-1&&y==currentGeneration.length-1){
如果(当前代[y-1][x]==1){
neighbourCounter++;
}
如果(当前代[y][x-1]==1){
neighbourCounter++;
}
如果(当前代[y-1][x-1]==1){
neighbourCounter++;
}
}else如果(y==0&&x==currentGeneration[0]。长度-1){
如果(当前代[y][x-1]==1){
neighbourCounter++;
}
如果(当前代[y+1][x]==1){
neighbourCounter++;
}
如果(当前代[y+1][x-1]==1){
neighbourCounter++;
}
}如果(y==0),则为else{
对于(inti=-1;i

public static int findNeighbors(int x, int y, int[][] a) {
    int sum = 0;
    for ( int i = (y>0 ? y-1 : 0); i <= (y<a.length-1 ? y+1 : a.length-1); ++i )
        for ( int j = (x>0 ? x-1 : 0); j <= (x<a[0].length-1 ? x+1 : a[0].length-1); ++j )
            sum += a[i][j];
    sum -= a[y][x];
    return sum;
}
public static int findNeighbors(int x,int y,int[]a){
整数和=0;

对于(int i=(y>0?y-1:0);我猜这是家庭作业??到目前为止你都做了些什么。如果你发布了一些代码,我们可以指导你是否做得正确,或者是否有更好的方法。不,我只是想知道是否有一种有效的方法来做这件事。我目前的方法是通过检查特定“中心”的位置来强制执行元素是(一个角、一个边或中间)然后在不超出数组边界的情况下检查可能的邻居。“这是我当前的暴力代码。myArray=currentGeneration”结尾的文本应该在那里,还是应该是现有代码的一部分?这只是从示例开始的:我的简短示例中的myArray是我代码中的currentGeneration数组。请注意第6行中的编辑:
sum-=a[y][x];
而不是
sum-=a[x][y];
责怪笛卡尔坐标的顺序与数组索引的顺序不同:)