HaspMap里的数据是怎么产生的

zzzkkk666 2008-08-02 02:44:19
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);
}
}

运行结果如下,我不明白的是HaspMap里的数据是怎么产生的,看起来是随机的
{15=7, 4=5, 19=4, 8=6, 11=3, 16=2, 18=4, 3=4, 7=5, 12=6, 17=1, 2=0, 13=5, 9=1, 6=9, 1=0, 14=3, 10=2, 5=7, 0=6}
...全文
137 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20080801/15/586e911e-5e22-4780-ad1d-a18fcd5e0972.html
。。。。
无语
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zzzkkk666 的回复:]
是随机产生的,谢了
[/Quote]
败了,,小分又给马甲了。。。
智商。
zzzkkk666 2008-08-02
  • 打赏
  • 举报
回复
是随机产生的,谢了
wykris 2008-08-02
  • 打赏
  • 举报
回复
if...else...

不满足if的条件时,执行else,向HashMap里添加指定的Key和Value

整个程序的意思是生成100个1-20之间的随机数,记录各数字出现的次数
  • 打赏
  • 举报
回复
而map中key是不允许有重复的,,写错了,刚才
ZangXT 2008-08-02
  • 打赏
  • 举报
回复
hashmap里面的数据不是你自己放进去的吗?
取的随机数,得到的当然是随机的了。
  • 打赏
  • 举报
回复

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中,一边添加,一边判断。


o了。。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧