关于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 sta
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/23 12:27:41
关于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 sta
关于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,然后分别统计各个数字出现的次数.package com.jl;import java.util.HashMap;import java.util.Iterator;import java.util.Random;import java.util.Set;public class Practice {public sta
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);
}
}
}