Java 在二维数组中填充随机数以进行列/行添加

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18839715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:33:23  来源:igfitidea点击:

Fill random numbers in a 2D array for column/row addition

javaarraysrandomfill

提问by Bmc

Hi folks new to Java and am making slow progress. I am trying to populate a 2D array with random numbers before I add the rows and columns. So far I can create the array, display it and I'm pretty sure I have the addition bit sorted. But I'm getting an outofboundsexception when I try to fill it with random numbers. Where am I going wrong?

大家好,Java 新手,进展缓慢。在添加行和列之前,我试图用随机数填充二维数组。到目前为止,我可以创建数组并显示它,而且我很确定我已经对添加位进行了排序。但是当我尝试用随机数填充它时,我得到了一个 outofboundsexception。我哪里错了?

public static void main(String[] args)
{
    //create the grid
    final int row = 9;
    final int col = 9;
    int [][] grid = new int [row][col];

    //fill the grid
    for (int i=0; i<grid.length; i++)
    grid[i][grid[i].length] = (int)(Math.random()*10);

    //display output
    for(int i=0;i<grid.length; i++)
    {
        for(int j=0; j<grid[i].length; j++)
        System.out.print(grid[i][j]+"");
        System.out.println();
    }

    int sum = 0;
    for (int i = 0; i < grid.length; i++) {
        System.out.println("This row sums up to: " + sum);


        for (int j = 0; j < grid[i].length; j++) {
            sum += grid[j][i];
        }
        System.out.println("This column sums up to: " + sum);
    }
}

采纳答案by arshajii

grid[i][grid[i].length] = (int)(Math.random()*10);

This will be an out-of-bounds exception. The maximum index of an array ais a.length - 1(since arrays are 0-indexed) -- you're trying to access an index of a.length. Here ais grid[i].

这将是越界异常。数组的最大索引aa.length - 1(因为数组是 0 索引的)——您正在尝试访问a.length. 这agrid[i]

In any case, if you want to fill the array fully, you'll need two for-loops:

无论如何,如果要完全填充数组,则需要两个for循环:

for (int i = 0; i < grid.length; i++) {
    for (int j = 0; j < grid[i].length; j++) {
        grid[i][j] = (int)(Math.random()*10);
    }
}

The outer for-loop loops over all the 1D arrays contained in the 2D array grid, and the inner for-loop fills each one of these inner 1D arrays with random values.

for循环遍历 2D array 中包含的所有一维数组grid,内循环for用随机值填充这些内部一维数组中的每一个。

Oh, and one last thing. When you calculate the sum, in the innermost loop, you have sum += grid[j][i]. You likely want ito be the array index and jto be the element index of the array at index i, i.e. grid[i][j].

哦,还有最后一件事。当您计算总和时,在最内层的循环中,您有sum += grid[j][i]. 您可能希望i成为数组索引并j成为索引处数组的元素索引i,即grid[i][j]

Also note that if you're not writing to the array (e.g. printing or finding the sum) you can use Java's enhanced for-loop as well:

另请注意,如果您不写入数组(例如打印或查找总和),您也可以使用 Java 的增强型for-loop:

int sum = 0;

for (int[] row : grid)
    for (int n : row)
        sum += n;

It's slightly less verbose and perhaps more legible.

它稍微不那么冗长,也许更清晰。