【问题标题】:Counting occurrences inside an array计算数组中的出现次数
【发布时间】:2015-06-18 05:35:01
【问题描述】:

按照建议,我回去了,开始查看几个月前我错误的一些代码,并一直在尝试更新它,以提高效率。

我想要的只是让我的方法返回一个包含出现次数的新数组。

例如: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

我知道问题发生在我分配数组[] numOccur 中的新元素时,仅仅是因为我的分配吗?我只是想知道我是否朝着正确的方向前进。

在我之前的版本中,我只使用 switch 语句,没有数组,所以这有点不同。

编辑 1:我应该发布我正在使用的主要方法!

weather.count(res) 其中 res 是我在班级上方发布的数组

/*这是我的第一篇文章 - 如果有人对措辞更好的问题有任何建议,请不要犹豫,我想要最清楚的,没有给出的答案

【问题讨论】:

  • 也许你只需要转换 count 我的意思是你应该这样做:numOccur[ (int)count ]++;
  • 这是因为它是一个整数,我需要它作为一个整数吗?无论如何,我仍然遇到与以前相同的错误
  • 应该是count( new int [] {2,4,0,7,4,2,1,0} ),因为方法count接受int的数组作为参数
  • 我已经给它传递了一个数组,不需要在方法中初始化一个新的
  • 使用HashMap 会更适合这个。

标签: java arrays


【解决方案1】:

您绝对可以按照@Elliott Frisch 的建议让您当前的实现工作,但您也可以采用不同的方式并使用HashMap

这是一个使用HashMap 的工作解决方案,它的额外好处是不需要遍历可能大量的空索引,因为只有初始数组中存在的数字才会在@987654323 中具有值@。

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 的数组,为了处理返回的数组,您需要遍历所有 100 万和 1 个索引,除了两个之外,所有这些都是空的。

【讨论】:

  • 未来,这会被认为是更有效的选择吗?公共静态 HashMap 会去哪里?在我的服务类或主类中?
  • 我唯一遇到的其他问题是它工作得很好,但现在我根本没有计数方法?
  • 我只是在考虑如何测试这个。你懂的?我有我的服务类和我所有的方法,计数被包含在其中,我有一个单独的主要方法,我想用它来测试它。例如,count({4,4,4,0,0,0,0,2}) 返回 {0 count: 3, 2 count: 1, etc}
  • ohhhhh 哇我看到这么瞎!不习惯在方法签名中看到 之类的东西。如果您不介意我的提问,而不是试图“讨论”,而是我看到了诸如 HashMap 和 Iterator 之类的东西,这些东西是什么?
  • 如果我想尝试而不是传递一个整数数组,只需说类似 obj.count(new Integer{1, 2, 3}) 之类的东西还是非法的?
【解决方案2】:

当您调用此行时:

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

这意味着您使用 nums.length 创建了一个新数组,因此您可以获得介于 0 和 nums.length-1 之间的索引,但是当您像这样为该数组赋值时,numOccur[count]++; count 是数组的值命名为 nums,count 可能大于 nums.length-1。您的程序可能会指出ArrayIndexOutOfBoundsException 的异常

【讨论】:

  • 所以我可能需要类似 numOccur[count[i]++;理论上?
【解决方案3】:

您需要根据输入数组中的最大值来调整输出数组的大小。您可以从一种方法开始,以查找数组中的最大值,例如

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)];

【讨论】:

  • 这会做什么?我有一个获得最大值的方法,但我不明白为什么我会在这里使用它,因为我的 nums 长度将是相同的,因为每个元素将至少出现一次,因为它在那里
  • @WilliamMabry 我相信您的代码会计算值 0n 的出现次数。在您的原始方法中,您假设 n 是数组 nums 的长度。我告诉你n 是数组numsmaxValue
猜你喜欢
  • 1970-01-01
  • 2017-03-24
  • 2015-06-19
  • 1970-01-01
  • 1970-01-01
  • 2015-04-15
  • 2021-09-02
相关资源
最近更新 更多