繁体   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?

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

这是我的代码:

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

}

尝试这个:

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

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

  1. 从使用第一行开始,或者使用无效数据作为“最大值”
  2. 检查其他值是否更大。 如果是这样,请用该数字替换“最大”计数(数量)和“行数”。

这是代码,它首先将值设置为第一行的最大值,然后检查其他行是否有更多零。

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

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

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


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