Java 如何在二维阵列中添加相邻单元?

Java 如何在二维阵列中添加相邻单元?,java,arrays,for-loop,multidimensional-array,Java,Arrays,For Loop,Multidimensional Array,我需要让第一个数组中的每个单元格成为所有相邻单元格的总和,然后将答案转储到第二个数组中。 例如: 带有随机数的初始数组: 3 5 11 5 9 14 1 2 8 计算数组: 19 42 41 20 49 48 33 62 44 3点([0][0])是5+9+5=19,依此类推。以下是我所拥有的: public class ProcessArray { private int rows; private int col

我需要让第一个数组中的每个单元格成为所有相邻单元格的总和,然后将答案转储到第二个数组中。 例如:

带有随机数的初始数组:

3   5   11  
5   9   14   
1   2   8 
计算数组:

19  42  41  
20  49  48  
33  62  44 
3点([0][0])是
5+9+5=19
,依此类推。以下是我所拥有的:

public class ProcessArray {

    private int rows;
    private int columns;
    private int [][] firstArray;
    private int [][] secondArray;


public ProcessArray(int rows, int columns) {

    this.rows=rows;
    this.columns=columns;
    firstArray = new int[rows][columns];
    secondArray = new int[rows][columns];

    initializeArray(firstArray, secondArray);

    randomlyFillArray(firstArray);
    System.out.println("Initial array with random numbers: ");
    printArray(firstArray, secondArray, rows, columns);
    getFirstArray(firstArray);
    System.out.println("Computed array:");
    computeArrayValues(firstArray);



}

private void initializeArray(int firstArray[][], int secondArray[][]){
    for(int i = 0; i <firstArray.length; i++){
        for (int j =0; j<firstArray[i].length; j++){
            firstArray[i][j] = (0);
        }

    }
    for(int i = 0; i <secondArray.length; i++){
        for (int j =0; j<secondArray[i].length; j++){
            secondArray[i][j] = (0);
        }

    }
}

public void randomlyFillArray(int firstArray[][]){
    for(int i = 0; i <firstArray.length; i++){
        for (int j =0; j<firstArray[i].length; j++){
            firstArray[i][j] = (int)(Math.random()*15);
        }

    }

}

//here's where I try to have it add, I don't know what loop to have it run to go to each spot in the `firstArray`:

public void computeArrayValues(int firstArray[][]){
    int x=1;
    int y=1;
    int sum;

    int topLeft = firstArray[x-1][y-1];
    int top = firstArray[x][y-1];
    int topRight = firstArray[x+1][y-1];
    int midLeft = firstArray[x-1][y];
    int midRight = firstArray[x+1][y];
    int botLeft = firstArray[x-1][y+1];
    int bot = firstArray[x][y+1];
    int botRight = firstArray[x+1][y+1];

    secondArray[0][0]= (bot+botRight+midRight);

    for (x=0; x<firstArray.length; x++){
        for(y=0; y<firstArray.length; y++){
    secondArray[x][y] = (topLeft+top+topRight+midLeft+midRight+botLeft+bot+botRight);
        }
    }
    System.out.println(secondArray[x][y]);
}

public void printArray(int firstArray[][], int secondArray[][], int rows, int columns){

    for (int i = 0; i < rows; i++){
        for (int j = 0; j < columns; j++){
            System.out.printf(String.format("%4s", firstArray[i][j]));
        }
        System.out.println();
    }

}

public int[][] getFirstArray(int array[][]){
    array = firstArray;
    return array;

}

public int[][] getSecondArray(int array[][]){
    array = secondArray;
    return array;

}
公共类ProcessArray{
私有int行;
私有int列;
私有int[]firstArray;
私有int[]secondArray;
公共ProcessArray(int行、int列){
这个。行=行;
this.columns=columns;
firstArray=新整数[行][列];
secondArray=newint[行][列];
初始化array(第一个数组,第二个数组);
随机填充数组(firstArray);
System.out.println(“带有随机数的初始数组:”);
printArray(第一个数组、第二个数组、行、列);
getFirstArray(firstArray);
System.out.println(“计算数组:”);
ComputeArrayValue(第一个数组);
}
私有void初始值设定项array(int firstArray[]],int secondArray[]]{

对于(int i=0;i假设您正在寻找关于替代方法的建议,我建议封装单元坐标并使用单元流而不是迭代。这假设java 8(自然):


这将满足规定的要求,但使用示例数据运行程序将输出:

194228
20 49 35 
16 37 25 
这将是所有相邻单元格总和的正确输出(如果我误解了问题,请告诉我)

公共类数组{
静态int[][]和(int数组[]]{
//分割方形阵列
int size=array.length;
int result[][]=新的int[size][size];
//对于表中的每个单元格
对于(int i=0;i
你的问题是什么?你是在征求对你的代码或其他方法的意见吗?你的示例初始数组和计算数组正确吗?根据你的解释,计算数组的左下角单元格应该是5+9+2=16。但是你说的是33。我没有正确理解你的问题吗?
class Cell {
    private final int row;
    private final int col;

    private Cell(int row, int col) {
        this.row = row;
        this.col = col;
    }

    public static Stream<Cell> streamCells(int rows, int cols) {
        return IntStream.range(0, rows)
            .flatMap(row -> IntStream.range(0, cols)
                .flatMap(col -> new Cell(row, col)));
    }

    public Stream<Cell> streamAdjacentCells(int rows, int cols) {
        return IntStream.range(row - 1, row + 1)
            .flatMap(row -> IntStream.range(col - 1, col + 1)
                .flatMap(col -> new Cell(row, col)))
            .filter(cell -> cell.row >= 0 && cell.col >= 0)
            .filter(cell -> cell.row < rows && cell.col < cols)
            .filter(cell -> cell.row != this.row && cell.col != this.col);
    }

    public int getValue(int[][] array) {
        return array[row][col];
    }

    public void setValue(int[][] array, int value) {
        array[row][col] = value;
    }
}
int[][] destination = new int[rows][cols];
Cell.streamCells(rows, cols)
    .forEach(cell -> setValue(
        destination,
        cell.streamAdjacentCells(rows, cols)
            .mapToInt(adj -> getValue(source, adj))
            .sum()));