Map
Integer[] ks = new Integer[50];
Random rand = new Random();
for (int i = 0; i < ks.length; i++) {// 赋值50个随机数
ks[i] = rand.nextInt(6) + 30;
}
for (int i = 0; i < ks.length; i++) {// 统计个数
if (map.get(ks[i]) == null) {
map.put(ks[i], 1);
} else {
map.put(ks[i], map.get(ks[i]) + 1);
}
}
List
Collections.sort(list);// 排序
Collections.reverse(list);// 降序排列
for (Integer num : list) {
System.out.println(num + " 个数: " + map.get(num));
}
import java.util.*;
public class Yugi {
public static void main(String[] args) {
Random random = new Random();
TreeMap < Integer, Integer > map = new TreeMap < Integer, Integer > (new Comparator(){
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
});
System.out.println("五十个数有:");
for (int i = 0; i < 50; i++) {
int n = random.nextInt(6) + 30;
if (null == map.get(n)) {
map.put(n, 1);
} else {
int x = map.get(n);
map.put(n, x + 1);
}
}
System.out.println(map);
}
}