关于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 {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 =

问题描述:

关于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 {
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);
}
}
}

class test{public static void main(String[]s){Random random = new Random();HashMapmap = new HashMap();for(int i = 0; i