簡體   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