在二维数组中替换行和列

3

我有一个二维数组:

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

3
你不能简单地看到一个零然后将该行/列清零。这意味着后续读取将看到你清零的行/列,你的矩阵将变成全零(就像你所见过的那样)。你需要在读完整个矩阵后记录你看到的零,然后返回并将其清零。 - Ryan J
这就是为什么我使用了一个临时数组。 - Owen Hunter
这是一个开始,但由于数组不仅仅通过将变量分配给另一个变量来复制,而只是复制引用。可能更容易的方法是创建一个新的数组来跟踪零位置。您可以使用1D数组进行索引,存储列的索引,然后从中清零。 - Ryan J
2个回答

2
问题出在这行代码上。
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];

1
在循环中检测到0后,您需要修改数据并继续循环,这将会看到更多的零并设置更多的零。您应该在发现0后立即停止循环,或者将检测和“重写”分开 - 先进行所有的检测,然后再进行所有的重写。

那是错误的 - 他正在寻找m中的0,然后更改tempArray中的值。问题在于数组复制,正如@pbabcdefp所指出的那样。 - J Richard Snape

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接