62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.*;
//这个代码里的数字是怎么生成的,还是不太理解
public class hh {
public static void main(String[] args){
HashMap h1=new HashMap();
Random r1=new Random();
for(int i=0;i<100;i++){
Integer t=new Integer(r1.nextInt(20));
if(h1.containsKey(t)){//如果包含指定的映射关系
((Ctime)h1.get(t)).count++;
//System.out.println("==="+((Ctime)h1.get(t)).count++);
}
else{
h1.put(t, new Ctime());//指定key和value
//System.out.println("---"+new Ctime());
}
}
System.out.println(h1);
}
}
class Ctime{
int count=0;
public String toString(){
return Integer.toString(count);
}
}
import java.util.*;
public class hh {
public static void main(String[] args){
HashMap h1=new HashMap();
Random r1=new Random(); ------大哥,你这不让她随即产生的么,当然是随即出来的啊。。。
for(int i=0;i<100;i++){------------循环了100下,就随即产生了100个呗。
Integer t=new Integer(r1.nextInt(20));----------转成int型
if(h1.containsKey(t)){//如果包含指定的映射关系
((Ctime)h1.get(t)).count++;//然后相应的value值++,
//System.out.println("==="+((Ctime)h1.get(t)).count++);
}
else{
h1.put(t, new Ctime());//指定key和value //如果不存在,就添加上。
//System.out.println("---"+new Ctime());
}
}
System.out.println(h1);
}
}
class Ctime{
int count=0;
public String toString(){
return Integer.toString(count);
}
}
------------------------------------
这个题的意思就是,让你随即产生100个20以内的数字,然后让你给出每个随即数产生的次数,而map中key是不运行有重复的,所以就添加到map中,一边添加,一边判断。