简体   繁体   English

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

[英]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?

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. 方法countZeros()对每一行中的零进行计数。 I need to compare each count with the next, so I created count and count2 . 我需要将每个计数与下一个进行比较,因此创建了countcount2 The location of the greater count will be stored in rowNum . 较大计数的位置将存储在rowNum I'm not sure what I am doing wrong. 我不确定自己在做什么错。 I think it may be indexing incorrectly. 我认为可能是索引编制错误。

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;
    }

}

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;
}

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. Check if the other values are greater. 检查其他值是否更大。 If they are, replace the 'max' count ( the amount) and the 'row number' with that one. 如果是这样,请用该数字替换“最大”计数(数量)和“行数”。

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. 原始方法的问题在于,它总是将count设置为下一个大于0的值,而没有与之前看到的实际最大0进行比较。 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) 因此,最后,它将仅返回至少具有一个0的最后一行(在本例中为第2行,第三行)

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. 您应该使用变量count2来存储到目前为止所有行中的最大零数,以便将其与下一行进行比较。 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 . 为此,如果“ count”大于count2 ,则需要在if块内为它分配当前计数。 I have redesigned your code below using maxCount and currentCount , for extra clarity. 为了更加清晰起见,我在下面使用maxCountcurrentCount重新设计了您的代码。 The second loop in your code is unnecessary, I didn't really understood why you did it. 您的代码中的第二个循环是不必要的,我真的不明白您为什么这样做。


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; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM