为啥加入了一行结果完全不同?
xindy 2002-07-22 03:38:18 程序内容如下:
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);
}
} ///:~
我在else后面 hm.put(r, new Counter());语句前面加入了
System.out.println(r);
显示的结果就完全不同了,为什么?