统计年龄分布情况问题
Posted 年少有为
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了统计年龄分布情况问题相关的知识,希望对你有一定的参考价值。
@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]); } } }
以上是关于统计年龄分布情况问题的主要内容,如果未能解决你的问题,请参考以下文章