没人理我,看得到源码吗?
import java.util.*;class Counter {
int i = 1;
public String toString() {
return Integer.toString(i);
}
}public class Statistics {
public static void main(String[] args) {
HashMap hm = new HashMap();
for(int i = 0; i < 10000; i++) {
// Produce a number between 0 and 20:
Integer r = new Integer((int)(Math.random() * 20));
if(hm.containsKey(r))
((Counter)hm.get(r)).i++;
else
hm.put(r, new Counter());
}
System.out.println(hm);
}
}
import java.util.*;class Counter {
int i = 1;
public String toString() {
return Integer.toString(i);
}
}public class Statistics {
public static void main(String[] args) {
HashMap hm = new HashMap();
for(int i = 0; i < 10000; i++) {
// Produce a number between 0 and 20:
Integer r = new Integer((int)(Math.random() * 20));
if(hm.containsKey(r))
((Counter)hm.get(r)).i++;
else
hm.put(r, new Counter());
}
System.out.println(hm);
}
}
解决方案 »
- 关于setVisible的疑问
- 如何能够对系统变量进行监听
- 为什么我把class文件都用jar.exe打包成jar文件,双击文件但不能运行?
- 各位高人如何利用JAVA程序在本地建立目录啊(望速答!马上结帐)
- 请教Socket问题,客户端取不到返回数据
- 如何能将带有“#”的字符串(例如“12345#”)写入sql2000中已定义为char的字段
- 一段小代码!急!在线等待!分不多望原谅!
- 一个关于环境变量设置的问题
- win98如何配jdk路径??
- 急,请问,java执行流程的顺序,谢谢!!!!
- 一个很简单的程序,照书上做的,但是却运行不了,这是怎么回事?
- 关于多线程的问题,为什么这个程序的增量为25,而不是1
int i = 1; public Counter(int i) {
this.i = i;
}
public String toString() {
return Integer.toString(i);
} public boolean equals(Object o) {
return ((o instanceof Counter) && (((Counter)o).i == i));
} public int hashCode() {
return i;
}
}public class Statistics {
public static void main(String[] args) {
HashSet hs = new HashSet();
for(int i = 0; i < 10000; i++) {
// Produce a number between 0 and 20:
Counter counter = new Counter((int)(Math.random() * 20));
if(hs.contains(counter)) {
//无法内部计数,因为HashSet没有直接的方法去获得包含的对象,它主要是排序,方便搜寻.
}
else {
hs.add(counter);
}
}
System.out.println(hs);
}
}