提示:
第一步:产生10000个随机数字,放入List集合中,然后统计
统计:使用Map存放统计结果,键是数字,值是出现次数
package ch09;
import java.util.*;
public class test02 {
public static void main(String[] args) {
Map n = new HashMap<>();
List c=new ArrayList();
Random r=new Random();
for (int i = 1; i <=10000 ; i++) {
int a=r.nextInt(10);
c.add(a);
}
String a=c.toString();
String b=a.substring(1,a.length()-1);
String d=b.replace(",","");
String f=d.replace(" ","");
char [] x=f.toCharArray();
for (int j = 0; j < x.length; j++) {
Object value=n.get(x[j]);
if (value==null){
n.put(x[j],1);
}else
{
int num=(Integer) value;
num++;
n.put(x[j],num);
}
}
System.out.println(n);
}
}