没人理我,看得到源码吗?
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);
}
}
解决方案 »
- 大家来做两道题练练手!!(简单)
- API文档
- 请大家推荐几本能够深入学习Java的好书,谢谢了 !
- 请教大家一个问题?
- test.tar.gz这种包是什么工具打的?
- 救急啊!!!!帮帮忙啊 新手开始写程序 难啊!!!在线等中
- 高手请进,关于调用servlet的问题,解决了散尽我所有的可用分(1500)
- ★JAVA的GUI程序如何做个确认提示框?★
- 定义一个String型空数组编译报错
- 用JB开发的Applet,在JB中正常运行,但移植到Apache服务器上时,无法显示并出错
- 请较Jbuilder中是否有类似delphi中的showmodal事件?
- 怎样从一个Class中取得ResultSet数据集
int i = 1;
public String toString() {
return Integer.toString(i);
}
public boolean equals(Object obj)
{
if (obj != null)
{
if (this.getClass().equals(obj.getClass()))
{
Counter that = (Counter) obj;
return (this.i == that.i);
}
}
return false;
}
public int hashCode()
{
return (i+"").hashCode();
}
}
import java.io.*;class Counter {
int i = 1;
public String toString() {
return Integer.toString(i);
}
}
public class testH { public testH() {
}
public static void main(String[] args) {
testH testH1 = new testH();
//HashMap hm = new HashMap();
HashSet hm=new HashSet();
HashMap hp=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.contains(r))
hm.add(r);
}
System.out.println(hm);
}}
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);
}
}