在阵列计数的数字发生 [英] counting occurrence of numbers in array

查看:188
本文介绍了在阵列计数的数字发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我堆栈一会儿。我试图调试,但我无法找出解决方案。我试图计算数字的发生。所以我的问题是,当我打印输出,是

  3出现1次
1时1倍
0时1次
2出现1次
1出现2次
3出现2次
2发生2次
0出现2次
10出现1次
4出现1次

而不是

  1出现2次
0出现2次
2发生2次
3出现2次
10出现1次
4出现1次

因此​​,如果数量已经超过1次,应该说这一次没有像很多倍有发生。干杯这里是code

 进口的java.util。*;公共类CountingOccuranceOfNumbers
{    公共静态无效的主要(字串[] args)
    {
        countNumbers();
    }    公共静态无效countNumbers()
    {
        扫描仪输入=新的扫描仪(System.in);
        随机数发生器=新的随机();
        INT []列表=新INT [11];
        INT [] =计数INT新[150];
        INT计数器= 0;
        INT数= 1;
        而(计数器< = 10)
        {
                数= generator.nextInt(11);
                列表[计数器] =号;
                反++;
        }
        的for(int i = 0; I< List.length的数字 - 1;我++)
        {
            计数[名单[I]] ++;
// System.out.print(名单[I] +);            的System.out.println(名单[I] +出现+计数[名单[I] +时代);
        }    }}


解决方案

另一个选择是番石榴的多集类,将跟踪计数你:

  int值中[] = ...;
多集<整数GT; MS = HashMultiset.create();
ms.addAll(Ints.asList(列表));INT COUNT0 = ms.count(Integer.valueOf(0));
INT的count1 = ms.count(Integer.valueOf(1));

下面,多集,HashMultiset和整型都番石榴类。

注意多集呢pretty多用地图和反跟踪柜台上面提到的什么人。这只是抽象离你远去,让您的code简单。

I am stack for a while . I tried debugging but I couldn't figure out the solution. I am trying to count the occurrence of numbers. So my problem is that when I print the output it is

3 occurs 1 times
1 occurs 1 times
0 occurs 1 times
2 occurs 1 times
1 occurs 2 times
3 occurs 2 times
2 occurs 2 times
0 occurs 2 times
10 occurs 1 times
4 occurs 1 times

instead of

1 occurs 2 times
0 occurs 2 times
2 occurs 2 times
3 occurs 2 time
10 occurs 1 times
4 occurs 1 times 

so if the number has more than 1 occurrence it should say it only once not as many times as there is occurrences. Cheers Here is the code

import java.util.*;

public class CountingOccuranceOfNumbers
{

    public static void main(String[] args) 
    {
        countNumbers();
    }

    public static void countNumbers()
    {
        Scanner input = new Scanner(System.in);
        Random generator = new Random();
        int[] list = new int[11];
        int[] counts = new int[150];
        int counter = 0;
        int number = 1;


        while(counter <= 10)
        {
                number = generator.nextInt(11);
                list[counter] = number;
                counter++;
        }   
        for(int i=0; i<list.length - 1; i++)
        {
            counts[list[i]]++;
//          System.out.print(list[i] + " ");

            System.out.println(list[i] +" occurs " +  counts[list[i]] + " times");
        }

    }

}

解决方案

Another option is guava's Multiset classes, which will track the count for you:

int values[] = ...;
Multiset<Integer> ms = HashMultiset.create();
ms.addAll(Ints.asList(list));

int count0 = ms.count(Integer.valueOf(0));
int count1 = ms.count(Integer.valueOf(1));

Here, Multiset, HashMultiset and Ints are all guava classes.

Note that Multiset does pretty much what someone mentioned above by using a Map and counter to track the counters. It's just abstracted away from you to make your code simpler.

这篇关于在阵列计数的数字发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆