简体   繁体   English

Java - 如何计算出现频率?

[英]Java - how to calculate the frequency of occurrence?

The method printearthquakes() is going to list each country for which there was one or more earthquakes as well as the number of earthquakes detected in that country.方法printearthquakes()将列出发生过一次或多次地震的每个国家以及在该国家检测到的地震次数。

I have two different lists one of which contains the name of all countries and the other one keeps record of earthquake information ie location, magnitude, etc. The expected outcome should look something like this我有两个不同的列表,其中一个包含所有国家的名称,另一个记录地震信息,即位置、震级等。预期结果应该是这样的

Nepal: 1
United States of America: 9
Uzbekistan: 1 

My program loops over the list of countries as well as earthquakes to find out if any country has happened to have one or more earthquakes and it attempts to print the name of that country and the number of earthquakes occurred there.我的程序循环遍历国家和地震列表,以找出是否有任何国家碰巧发生了一次或多次地震,并尝试打印该国家/地区的名称以及那里发生的地震次数。

private void printearthquakes() 
{

    int numOfQuakes = 0;

    if (quakeMarkers.size()>0)
    {
      for (Marker country: countryMarkers)
       {
        boolean firstpass = false;
        for(Marker quake : quakeMarkers)
        {

            if ( country.getProperty("name").equals(quake.getProperty("country")))
            {
                numOfQuakes++;
                System.out.println(country.getProperty("name"));
                System.out.println(numOfQuakes);

            }   
        }           
    }       
  }
}

All it does is repeating the country names for as many times as they appear in the earthquake list and counting from 0 :它所做的只是重复出现在地震列表中的国家名称,并从 0 开始计数:

Nepal
1
United States of America
2
United States of America
3
United States of America
4
United States of America
5
United States of America
6
United States of America
7
United States of America
8
United States of America
9
United States of America
10
Uzbekistan
11

I'd like to know where I am getting this wrong and how to fix it!我想知道我哪里出错了以及如何解决它!

you need to re-initialize counter for each country and display result only once per country, like this:您需要为每个国家重新初始化计数器,并且每个国家只显示一次结果,如下所示:

private void printearthquakes() {
    for (Marker country: countryMarkers) {
        int numOfQuakes = 0;
        for(Marker quake : quakeMarkers)
            if (country.getProperty("name").equals(quake.getProperty("country")))
                numOfQuakes++;
        System.out.println(country.getProperty("name"));
        System.out.println(numOfQuakes);
    }
}

NOTE: this is O(n^2) version, to speed-up process I'd suggest another approach:注意:这是 O(n^2) 版本,为了加快进程,我建议采用另一种方法:

private void printearthquakes() {
    Map<String, Long> quakes = new HashMap<>();
    for(Marker quake : quakeMarkers) {
        Long cnt = quakes.get(quake.getProperty("country"));
        quakes.put(quake.getProperty("country"), (cnt!=null?cnt.longValue():0) + 1L);
    }
    for (Entry<String, Long> e: quakes.entrySet()) {
        System.out.println(e.getKey());
        System.out.println(e.getValue());
    }
}

which is O(n) only这只是 O(n)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM