如果二维数组中存在零,则将该行置零。

huangapple go评论71阅读模式
英文:

Zero a row in a 2D array if the number zero exists in it

问题

以下是翻译好的代码部分:

import java.util.Arrays;

public class TwoDarr {
    public static void ZeroNum(int[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            boolean containsZero = false;
            for (int j = 0; j < arr[i].length; j++) {
                if (arr[i][j] == 0) {
                    containsZero = true;
                    break;
                }
            }
            if (containsZero) {
                Arrays.fill(arr[i], 0);
            }
        }
    }

    public static void main(final String[] args) {
        int[][] arr = {{144, 2, 3, 2, 5, 0},
                       {2, 36, 1, 2, 1, 6},
                       {0, 0, 9, 0, 3, 5},
                       {4, 4, 4, 225, 3, 4},
                       {1, 1, 1, 1, 16, 1}};
        ZeroNum(arr);

        for (int i = 0; i < arr.length; i++) {
            System.out.println(Arrays.toString(arr[i]));
        }
    }
}
英文:

I need to write something that will get an MxN 2D array, go over each row and search for the number 0. If the number zero is present in that row, the whole row will be set to 0's.

So for example, this:

int[][] arr ={{ 144, 2, 3, 2, 5, 0},
              {2, 36, 1, 2, 1, 6},
              {0, 0, 9, 0, 3, 5},
              {4, 4, 4, 225, 3, 4},
              {1, 1, 1, 1, 16, 1}};

Will turn into this:

int[][] arr ={{ 0, 0, 0, 0, 0, 0},
              {2, 36, 1, 2, 1, 6},
              {0, 0, 0, 0, 0, 0},
              {4, 4, 4, 225, 3, 4},
              {1, 1, 1, 1, 16, 1}};

I haven't made much progress. All my attempts at setting the row to zero either didn't work or zeroed out everything in the array.

import java.util.Arrays;

public class TwoDarr { 
    public static void ZeroNum(int[][] arr) {

        for (int i = 0; i &lt; arr.length; i++) {
                for (int j = 0; j &lt; arr[i].length; j++) {
                    if (arr[i][j] == 0) {
                        ???
                    }
                }
                }
            }
        public static void main(final String[] args) {
            int[][] arr ={{ 144, 2, 3, 2, 5, 0},
                            {2, 36, 1, 2, 1, 6},
                            {0, 0, 9, 0, 3, 5},
                            {4, 4, 4, 225, 3, 4},
                            {1, 1, 1, 1, 16, 1}};
        ZeroNum(arr);

        for (int i = 0; i &lt; arr.length; i++) {
			System.out.println(Arrays.toString(arr[i]));
		}
        }
    }

Any help on the matter would be great,
Thanks 如果二维数组中存在零,则将该行置零。

答案1

得分: 1

使用Arrays.fill方法将行填充为0。请注意,在Java中,二维数组是由一维数组构成的数组。

演示:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[][] arr = {{144, 2, 3, 2, 5, 0},
                       {2, 36, 1, 2, 1, 6},
                       {0, 0, 9, 0, 3, 5},
                       {4, 4, 4, 225, 3, 4},
                       {1, 1, 1, 1, 16, 1}};
        for (int[] row : arr) {
            for (int col : row) {
                if (col == 0) {
                    Arrays.fill(row, 0);
                    break;
                }
            }
        }
        for (int[] row : arr) {
            System.out.println(Arrays.toString(row));
        }
    }
}

输出:

[0, 0, 0, 0, 0, 0]
[2, 36, 1, 2, 1, 6]
[0, 0, 0, 0, 0, 0]
[4, 4, 4, 225, 3, 4]
[1, 1, 1, 1, 16, 1]

如下所示,您可以在不使用增强的for循环的情况下实现,但我建议您使用上面提供的更简洁的解决方案:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[][] arr = {{144, 2, 3, 2, 5, 0},
                       {2, 36, 1, 2, 1, 6},
                       {0, 0, 9, 0, 3, 5},
                       {4, 4, 4, 225, 3, 4},
                       {1, 1, 1, 1, 16, 1}};
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (arr[i][j] == 0) {
                    arr[i] = new int[arr[i].length];
                    break;
                }
            }
        }
        for (int[] row : arr) {
            System.out.println(Arrays.toString(row));
        }
    }
}

这个替代方案只有在元素必须被更改为0(这是int的默认值)的情况下才适用。

英文:

Use Arrays.fill to fill the row with 0s. Note that a 2-D array in Java is an array of 1-D arrays.

Demo:

import java.util.Arrays;

public class Main {
	public static void main(String[] args) {
		int[][] arr ={{ 144, 2, 3, 2, 5, 0},
	              {2, 36, 1, 2, 1, 6},
	              {0, 0, 9, 0, 3, 5},
	              {4, 4, 4, 225, 3, 4},
	              {1, 1, 1, 1, 16, 1 } };
		for (int[] row : arr) {
			for (int col : row) {
				if (col == 0) {
					Arrays.fill(row, 0);
                    break;
				}
			}
		}
		for (int[] row : arr) {
			System.out.println(Arrays.toString(row));
		}
	}
}

Output:

[0, 0, 0, 0, 0, 0]
[2, 36, 1, 2, 1, 6]
[0, 0, 0, 0, 0, 0]
[4, 4, 4, 225, 3, 4]
[1, 1, 1, 1, 16, 1]

As given below, you can achieve it without using the enhanced for loop but I suggest you use the cleaner solution given above:

import java.util.Arrays;

public class Main {
	public static void main(String[] args) {
		int[][] arr ={{ 144, 2, 3, 2, 5, 0},
	              {2, 36, 1, 2, 1, 6},
	              {0, 0, 9, 0, 3, 5},
	              {4, 4, 4, 225, 3, 4},
	              {1, 1, 1, 1, 16, 1 } };
		for(int i=0; i&lt;arr.length; i++) {
			for (int j=0; j&lt;arr[i].length; j++) {
				if(arr[i][j] == 0) {
					arr[i] = new int[arr[i].length];
                    break;
				}
			}
		}
		for (int[] row : arr) {
			System.out.println(Arrays.toString(row));
		}
	}
}

This alternative solution will work only as long as the elements have to be changed to 0 (which is the default value of an int).

答案2

得分: 0

public static void main(String[] args) {
    int[][] arr2d ={{...}};
    for (int i = 0; i < arr2d.length; i++) {
        if (containsZero(arr2d[i])) {
            arr2d[i] = new int[arr2d[i].length];
        }
    }
}

public static boolean containsZero(int []arr) {
    for (int n : arr) {
        if (n == 0) {
            return true;
        }
    }
    return false;
}
英文:
public static void main(String[] args) {
    int[][] arr2d ={{...}};
    for (int i = 0; i &lt; arr2d.length; i++) {
        if (containsZero(arr2d[i])) {
            arr2d[i] = new int[arr2d[i].length]]
        }
    }
}

public static boolean containsZero(int []arr) {
    for (int n : arr) {
        if (n == 0) {
            return true;
        }
    }
    return false;
}

答案3

得分: 0

public static void main(String[] args) {
    int[][] arr ={{ 144, 2, 3, 2, 5, 0},
                {2, 36, 1, 2, 1, 6},
                {0, 0, 9, 0, 3, 5},
                {4, 4, 4, 225, 3, 4},
                {1, 1, 1, 1, 16, 1 } };

    for(int i=0; i<arr.length; i++) {
        for (int j=0; j<arr[i].length; j++) {
            if(arr[i][j] == 0) {
                arr[i]=new int[arr[i].length];
                break;
            }
        }
    }
    for (int[] row : arr) {
        System.out.println(Arrays.toString(row));
    }
}
英文:
public static void main(String[] args) {
		int[][] arr ={{ 144, 2, 3, 2, 5, 0},
                {2, 36, 1, 2, 1, 6},
                {0, 0, 9, 0, 3, 5},
                {4, 4, 4, 225, 3, 4},
                {1, 1, 1, 1, 16, 1 } };
		
      for(int i=0; i&lt;arr.length; i++) {
          for (int j=0; j&lt;arr[i].length; j++) {
              if(arr[i][j] == 0) {
                 arr[i]=new int[arr[i].length];
                  break;
              }
          }
      }
      for (int[] row : arr) {
          System.out.println(Arrays.toString(row));
      }


	}

huangapple
  • 本文由 发表于 2020年4月8日 20:32:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/61100765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定