【问题标题】:Replacing rows and columns in a 2d array替换二维数组中的行和列
【发布时间】:2015-01-14 23:36:46
【问题描述】:

我有一个二维数组:

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

我必须编写一个程序来检查数组中是否有 0,如果有,请将行和列替换为 0,这样之后的结果如下所示:

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

这是我目前的代码:

public class Run {


    public static void main(String[] args){
        //defining 2d array
        int[][] m = { {1,0,1,1}, 
                      {1,1,1,1},
                      {1,1,1,1},
                      {1,1,1,1}}; 
        int[][] newArray = zero(m);
        //looping through the array to get rows and columns for the array
        //rows
        for (int i = 0; i < m.length; i++) {
            //columns
            for (int j = 0; j < m[0].length; j++) { 
                //check if the integer is the last in the row
                if(j== m.length-1){
                    //print the rows and columns of the array(no space)
                    System.out.print(newArray[i][j]);
                }else{
                    //print the rows and columns of the array(w/ space)
                    System.out.print(newArray[i][j] + " ");
                }
            }
            //new line for the new row
        System.out.println("");
        }
    }

    //checks if there is a zero in the row
    public static int[][] zero(int[][] m) {
        //defining row length and column length
        int rows = m.length;
        int columns = m[0].length;
        int[][] tempArray = m;

        //looping through the array to get rows and columns
        //rows
        for (int i = 0; i < rows; i++) {
            //columns
            for (int j = 0; j < columns; j++) {
                //if the number is 0 loop through that row and column again and change everything to 0 
                if(m[i][j] == 0){
                    //columns in that row
                    for(int l = 0; l < rows; l++)
                    {
                        tempArray[l][j] = 0;
                    }
                    //rows in that column
                    for(int l = 0; l < columns; l++)
                    {
                        tempArray[i][l] = 0;
                    }
                }
            }
        }

        //returning the updated array
        return tempArray;
}

}

当我运行我的代码时,它会返回:

0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

但是当我拿出任何一个时:

    //columns in that row
for(int l = 0; l < rows; l++)
{
    tempArray[l][j] = 0;
}

    //rows in that column
for(int l = 0; l < rows; l++)
{
    tempArray[l][j] = 0;
}

它返回:

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

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

【问题讨论】:

  • 您不能简单地看到一个零,然后将行/列归零。这意味着后续读取将看到您的零行/列,并且您的矩阵将变为全 0(如您所见)。读取整个矩阵后,您需要跟踪看到的零,然后返回并将它们归零。
  • 这就是我使用临时数组的原因。
  • 这是一个开始,但由于数组不是简单地通过将变量分配给另一个变量来复制的,因此只会复制引用。只创建一个新数组来跟踪零位置可能会更容易。您可以为此使用一维数组,按行索引,存储列的索引,然后从那里归零。

标签: java arrays multidimensional-array


【解决方案1】:

问题是线

int[][] tempArray = m;

这使得tempArraym 成为完全相同的实例,因此您实际上只有一个矩阵。

你应该这样做

int[][] tempArray = new int[rows][columns];
for (int i = 0; i < rows; i++)
    for (int j = 0; j < columns; j++)
        tempArray[i][j] = m[i][j];

【讨论】:

    【解决方案2】:

    您在循环中检测到 0,然后去修改数据并继续您的循环,现在将看到更多的零,因此设置更多的零。

    您应该在发现 0 后中断,或者将检测和“重写”分开 - 先进行所有检测,然后再进行所有重写。

    【讨论】:

    • 错了——他正在寻找 m 中的 0,然后更改 tempArray 中的值。问题在于数组复制为@pbabcdefp 标识。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    相关资源
    最近更新 更多