如何创建一个方法,将二维数组作为参数,并显示最多零的行的索引?

时间:2022-10-07 21:28:24

How do I create a method that takes a two-dimensional array as a parameter and displays the index of the row with the most zeros? The program I have does compile. It just displays an incorrect result. The method countZeros() counts the number of zeros in each row. I need to compare each count with the next, so I created count and count2. The location of the greater count will be stored in rowNum. I'm not sure what I am doing wrong. I think it may be indexing incorrectly.

如何创建一个方法,将二维数组作为参数,并显示最多零的行的索引?我有的程序编译。它只显示不正确的结果。方法countZeros()计算每行中的零个数。我需要将每个计数与下一个计数进行比较,因此我创建了count和count2。更大计数的位置将存储在rowNum中。我不确定我做错了什么。我认为它可能索引不正确。

Here is my code:

这是我的代码:

public class P118 
{

    public static void main(String[]args)
    {

        int[][]num = {{0,3,6,0,0}, {1,3,8,9,8}, {9,9,9,0,8}, {3,7,9,9,9}}; 

    System.out.print(rowWithMostZeros(num));

    }

    public static int rowWithMostZeros(int[][]arr)
    {
        int count = 0, count2 = 0, rowNum = -1;

        for(int row = 0; row<arr.length;row++)
        {

            count = countZeros(arr[row]);

            if(count>count2)
            {

            rowNum = row;
            }

        }
        for(int i=0; i<arr.length;i++)
        {

            count2 = countZeros(arr[i]);

        }

        return rowNum;
    }
    public static int countZeros(int[]x)
    {
        int count = 0;

        for(int i = 0; i<x.length;i++)
        {
            if(x[i]==0)
            {
              count++;

            }
        }


        return count;
    }

}

3 个解决方案

#1


0  

Try this:

public static int rowWithMostZeros(int[][] arr) {

    if (arr == null || arr.length < 1) {
        return -1;
    }

    int rowWithMostZeros = 0;
    int count = countZeros(arr[0]);

    for (int i = 1; i < arr.length; i++) {
        int count2 = countZeros(arr[i]);
        if (count2 > count) {
            rowWithMostZeros = i;
        }
    }

    return rowWithMostZeros;
}

public static int countZeros(int[] arr) {
    int count = 0;
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == 0) {
            count += 1;
        }
    }
    return count;
}

#2


0  

Any code that counts occurrences could work with the following method:

任何计算出现次数的代码都可以使用以下方法:

  1. Start with with either using the first row, or invalid data as the 'max'
  2. 首先使用第一行,或使用无效数据作为'max'

  3. Check if the other values are greater. If they are, replace the 'max' count ( the amount) and the 'row number' with that one.
  4. 检查其他值是否更大。如果是,请将“最大”计数(金额)和“行号”替换为该值。

Here is the code that first sets the values to the first row being the max, and then checks if any of the other rows have more zeros.:

下面的代码首先将值设置为第一行的最大值,然后检查其他任何行是否有更多的零:

public static int rowWithMostZeros(int[][]arr)
    {
        int mostZeroCount = countZeros(arr[0];
        int rowNum = 0;

        for(int row = 1; row<arr.length;row++)
        {
            int count = countZeros(arr[row]);

            if(count>mostZeroCount)
            {
                rowNum = row;
                mostZeroCount = count;
            }

        }
        return rowNum;
    }

The problem with the original method was that it always set count to be the next value which had more then one 0, without comparing to the actual maximum amount of 0's previously seen. Therefore, at the end, it would simply return the last row that had at least one 0 (Which in this case was Row 2, the third row)

原始方法的问题在于,它始终将count设置为下一个具有多于0的值,而不与之前看到的实际最大值0进行比较。因此,最后,它将返回至少有一个0的最后一行(在这种情况下是第2行,第三行)

#3


0  

You should use the variable count2 to store the maximum number of zeros in all rows so far, in order to compare it with the next row. In order to do this, you need to assign it the current count inside the if block, in case the "count" is bigger than count2. I have redesigned your code below using maxCount and currentCount, for extra clarity. The second loop in your code is unnecessary, I didn't really understood why you did it.

您应该使用变量count2来存储到目前为止所有行中的最大零数,以便将其与下一行进行比较。为了做到这一点,你需要在if块中为它分配当前计数,以防“count”大于count2。为了更加清晰,我使用maxCount和currentCount重新设计了下面的代码。代码中的第二个循环是不必要的,我真的不明白你为什么这么做。

 
public static int rowWithMostZeros(int[][]arr){
        int currentCount = 0, maxCount = 0, rowNum = -1;
        for(int row = 0; row maxCount){
                maxCount = currentCount;
                rowNum = row;
        }
        return rowNum;
}

public static int countZeros(int[]x){ int count = 0; for(int i = 0; i<x.length;i++){ if(x[i]==0){ count++; } } return count; }

#1


0  

Try this:

public static int rowWithMostZeros(int[][] arr) {

    if (arr == null || arr.length < 1) {
        return -1;
    }

    int rowWithMostZeros = 0;
    int count = countZeros(arr[0]);

    for (int i = 1; i < arr.length; i++) {
        int count2 = countZeros(arr[i]);
        if (count2 > count) {
            rowWithMostZeros = i;
        }
    }

    return rowWithMostZeros;
}

public static int countZeros(int[] arr) {
    int count = 0;
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == 0) {
            count += 1;
        }
    }
    return count;
}

#2


0  

Any code that counts occurrences could work with the following method:

任何计算出现次数的代码都可以使用以下方法:

  1. Start with with either using the first row, or invalid data as the 'max'
  2. 首先使用第一行,或使用无效数据作为'max'

  3. Check if the other values are greater. If they are, replace the 'max' count ( the amount) and the 'row number' with that one.
  4. 检查其他值是否更大。如果是,请将“最大”计数(金额)和“行号”替换为该值。

Here is the code that first sets the values to the first row being the max, and then checks if any of the other rows have more zeros.:

下面的代码首先将值设置为第一行的最大值,然后检查其他任何行是否有更多的零:

public static int rowWithMostZeros(int[][]arr)
    {
        int mostZeroCount = countZeros(arr[0];
        int rowNum = 0;

        for(int row = 1; row<arr.length;row++)
        {
            int count = countZeros(arr[row]);

            if(count>mostZeroCount)
            {
                rowNum = row;
                mostZeroCount = count;
            }

        }
        return rowNum;
    }

The problem with the original method was that it always set count to be the next value which had more then one 0, without comparing to the actual maximum amount of 0's previously seen. Therefore, at the end, it would simply return the last row that had at least one 0 (Which in this case was Row 2, the third row)

原始方法的问题在于,它始终将count设置为下一个具有多于0的值,而不与之前看到的实际最大值0进行比较。因此,最后,它将返回至少有一个0的最后一行(在这种情况下是第2行,第三行)

#3


0  

You should use the variable count2 to store the maximum number of zeros in all rows so far, in order to compare it with the next row. In order to do this, you need to assign it the current count inside the if block, in case the "count" is bigger than count2. I have redesigned your code below using maxCount and currentCount, for extra clarity. The second loop in your code is unnecessary, I didn't really understood why you did it.

您应该使用变量count2来存储到目前为止所有行中的最大零数,以便将其与下一行进行比较。为了做到这一点,你需要在if块中为它分配当前计数,以防“count”大于count2。为了更加清晰,我使用maxCount和currentCount重新设计了下面的代码。代码中的第二个循环是不必要的,我真的不明白你为什么这么做。

 
public static int rowWithMostZeros(int[][]arr){
        int currentCount = 0, maxCount = 0, rowNum = -1;
        for(int row = 0; row maxCount){
                maxCount = currentCount;
                rowNum = row;
        }
        return rowNum;
}

public static int countZeros(int[]x){ int count = 0; for(int i = 0; i<x.length;i++){ if(x[i]==0){ count++; } } return count; }