1.产生10000个0-9之间的随机数字,统计每个数字出现的个数。
package Java0910;
import java.util.*;
//产生10个0-9之间的随机数字,统计每个数字出现的个数。
public class Test02 {
public static void main(String[] args) {
List<Integer> l = new ArrayList<>();
Random r = new Random();
for (int i = 0; i < 10; i++) {
l.add(r.nextInt(10));
}
System.out.println(l);
Map<Integer, Integer> m = new HashMap<>();
for (Integer n : l) {
Integer v = m.get(n);
if (v == null) {
m.put(n, 1);
} else {
v++;
m.put(n, v);
}
}
System.out.println(m);
}
}