开发者

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. 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 d开发者_StackOverflowoing 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. 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)


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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜