我只是想用HashSet测试随机数的分布密度,请各位看看下面这个程序有什么问题??
//:用HashSet测试随机数的分布密度
import java.util.*;
class count implements Comparable { //定义Set对象
public static int cnt;
private int i = 1;
public count(int num) {
i = num;
}
public boolean equals(Object a) {
return (a instanceof count) && (((count)a).i == i);
}
public int compareTo(Object a) {
int ai = ((count)a).i;
return((ai > i) ? 1 : ((ai == i) ? 0 : -1));
}
public int hashCode() {
return i;
}
public String toString() {
return "count" + i +" cnt:" + cnt + "\n";
}
}
public class Randtestset { //主函数main
public static void main(String[] args) {
HashSet hs = new HashSet();
Iterator it = hs.iterator();
boolean added = false;
int Randint;
count cn;
count currElem;
for(int i = 0; i < 10; i++) {
Randint = (int)(Math.random() * 20);
System.out.println(Randint);
cn = new count(Randint);
it = hs.iterator();
while(it.hasNext()) {
System.out.println("one");
currElem = (count)it.next();
if(cn.equals(currElem)) {
currElem.cnt++;
added = true;
}
}
if(!added) {
hs.add(cn);
added = false;
}
}
}
}