关于Java的HashMap使用.想产生40个随机数,从10-50,然后分别统计各个数字出现的次数.
package com.jl;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
public class Practice {
public static void main(String[] args) {
HashMap map = new HashMap();
for (int i = 0; i < 50; i++)
{
Random r = new Random();
int num = (r.nextInt(41) + 10);
map.put(new Integer(i),new Integer(num));
System.out.println(num);
}
HashMap map2 = new HashMap();
Set set = map.keySet();
Set set2 = map.keySet();
for (Iterator iter = set.iterator(); iter.hasNext();)
{
Integer key = (Integer) iter.next();
Integer value = (Integer) map.get(key);
Iterator iter2 = set2.iterator();
Integer key2 = (Integer) iter2.next();
Integer abc = (Integer) map2.get(key2);
if (abc == null)
{
int k = 1;
map2.put(value,new Integer(k));
} else
{
Integer in = (Integer)map2.get(key2);
in = new Integer(in.intValue() + 1);
map.put(value,in);
}
}
Set abc = map2.keySet();
for (Iterator iter = abc.iterator(); iter.hasNext();)
{
Integer key = (Integer) iter.next();
Integer value = (Integer) map2.get(key);
System.out.println(key + : + value);
}
}
}
关于Java的HashMap使用.想产生40个随机数,从10-50,然后分别统计各个数字出现的次数.
答案:2 悬赏:30 手机版
解决时间 2021-12-24 20:12
- 提问者网友:温柔港
- 2021-12-24 04:17
最佳答案
- 五星知识达人网友:人间朝暮
- 2021-12-24 04:58
class test{
public static void main(String[]s){
Random random = new Random();
HashMapmap = new HashMap();
for(int i = 0; i < 40; i++){
map.put(i + 10,0);
}
for(int i = 0; i < 40; i++){
int num = random.nextInt(40) + 10;
map.put(i + 10, num);
}
}
}
全部回答
- 1楼网友:举杯邀酒敬孤独
- 2021-12-24 06:03
好好学习下
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯