簡體   English   中英

計算數組中的出現次數

[英]Counting occurrences inside an array

按照建議,我回去了,幾個月前我開始看一些錯誤的代碼,並一直在嘗試對其進行更新,以便提高效率。

我想要的只是讓我的方法返回其中包含所有出現的新數組。

例如: count({2,4,0,7,4,2,1,0}) returns {2,1,2,0,2,0,0,1}

到目前為止,我的方法是這樣,僅出於記錄目的,我想觀眾也喜歡,我正在使用的數組是這樣。

18  29  23  35  23  28  36  27  16  21  33  21  18  33  16  6  19  22  9  26  28  16  19  14  18  12  17  28

這是我的伯爵班

public int[] count(Integer[] nums)
{
    Integer[] numOccur = new Integer[nums.length]; //new array that will be returned with the occurances

    for (int i = 0; i < nums.length; ++i) //goes through the array for nums.length times.
    {
        Integer count = nums[i]; //assigns count to nums[i]
        numOccur[count]++;         //ERROR
        System.out.println(numOccur[i]); //testing to see if i'm printing the right value
    }

    return numOccur;
}

我正在

at WeatherSrv.count(WeatherSrv.java:94)
    at WeatherDrv.main(WeatherDrv.java:55)
Java Result: 1

我知道問題出在我將我的array [] numOccur中的新元素分配時發生了,這僅僅是因為我的分配嗎? 我只想知道我是否朝着正確的方向前進。

在我以前的版本中,我只使用switch語句,沒有數組,所以這有點不同。

編輯1:我應該發布我正在使用它的主要方法!

weather.count(res)其中res是我在課程上方的頂部發布的數組

/ *這是我的第一個帖子-如果有人對措辭更好的問題有任何建議,請不要猶豫,我想得到最清晰,沒有給出的答案

您需要根據輸入數組中的最大值確定輸出數組的大小。 您可以從在數組中找到最大值的方法開始

private static int getMaxValue(Integer[] nums) {
    int max = nums[0];
    for (int i = 1; i < nums.length; i++) {
        max = Math.max(max, nums[i]);
    }
    return max;
}

然后您的方法應調整其輸出數組的大小,例如

// You return an `int[]`
int[] numOccur = new int[1+getMaxValue(nums)];

當您撥打此行時:

Integer[] numOccur = new Integer[nums.length]

這意味着您將創建一個具有nums.length的新數組,因此可以獲得0到nums.length-1之間的索引,但是當您為該數組分配一個值時,例如numOccur[count]++; count是一個名為nums的數組的值,count可能大於nums.length-1。 您的程序可能會指出ArrayIndexOutOfBoundsException的異常

您可以使用@Elliott Frisch建議的方法來確定當前的實現,但是也可以使用另一種方法,並使用HashMap

這是一個使用HashMap解決方案,它的另一個好處是不需要迭代潛在的大量空索引,因為只有初始數組中存在的數字才會在HashMap具有值。

import java.util.HashMap;

public class CountOccur
{
  public static void main(String[] args)
  {
    Integer[] arr = new Integer[]{2,4,0,7,4,2,1,0};
    HashMap<Integer, Integer> countMap = count(arr);

    for (Integer key : countMap.keySet()) {
      System.out.println(key + " count: " + countMap.get(key));
   }

  }

  public static HashMap<Integer, Integer> count(Integer[] nums)
  { 
      HashMap<Integer, Integer> countMap = new HashMap<Integer, Integer>();

      for (int i = 0; i < nums.length; ++i)
      {
          Integer count = countMap.get(nums[i]); 
          if (count  == null){
            //first occurrence, enter a value of 1 for count
            countMap.put(nums[i], 1);
          }
          else{
            //already in the HashMap, increment the count
            countMap.put(nums[i], count + 1);
          }

      }

      return countMap;
  }
}

輸出:

0 count: 2
1 count: 1
2 count: 2
4 count: 2
7 count: 1

為了解決原始方法的弊端,請想象一下輸入: {1,1000000} 您將需要一個大小為1000001的數組,並且要處理返回的數組,您將需要遍歷所有一百萬個索引和一個索引,除了兩個索引以外,其余所有索引均為空。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM