zoukankan      html  css  js  c++  java
  • 统计年龄分布情况问题

    @Slf4j
    public class AgeTotal {
    
        public static String file = "/Desktop/age.txt";
    
        public static int total = 10000 * 10000;
    
        public static int maxAge = 180;
    
        public static Random random = new Random();
    
        public static void main(String[] args) throws Exception {
            long start = System.currentTimeMillis();
            productData();
            ageTotal();
            log.info("总用时:{}ms", System.currentTimeMillis() - start);
        }
    
        /**
         * 创建age文件
         *
         * @throws IOException
         */
        public static void productData() throws IOException {
            OutputStreamWriter outputStream = new OutputStreamWriter(new FileOutputStream(new File(file)), "UTF-8");
            BufferedWriter bufferedWriter = new BufferedWriter(outputStream);
            for (int i = 0; i < total; i++) {
                if (i != 0) {
                    bufferedWriter.newLine();
                }
                bufferedWriter.write(random.nextInt(maxAge + 1) + "");
            }
            bufferedWriter.flush();
            bufferedWriter.close();
        }
    
        /**
         * 统计年龄数据分布
         *
         * @throws Exception
         */
        public static void ageTotal() throws Exception {
            int[] ageArray = new int[maxAge + 1];
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File(file)), "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                ageArray[Integer.valueOf(str)]++;
            }
            for (int i = 0; i < ageArray.length; i++) {
                System.out.println("年龄:" + i + ",人数:" + ageArray[i]);
            }
        }
    
    }
    
    缘于生活,而归于工作。本人所书,而意于分享。 如有转载,请注明出处! --活出自己范儿
  • 相关阅读:
    php-Zip打包文件
    PHP命令行类库 climate
    vim 添加块注释
    冒泡排序|插入排序
    PHP-SeasLog安装和使用
    链表
    多线程上下文切换
    竞态与线程安全
    线程的生命周期
    线程创建的两种方法
  • 原文地址:https://www.cnblogs.com/Small-sunshine/p/14886181.html
Copyright © 2011-2022 走看看