之前那个输出的101个数组成的序列是去重复的,这个是保留重复的import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
public class RandomInMap {
//盛放结果的set
private TreeMap map=new TreeMap();
//随机数的起始值,默认用0
private int start=0;
//随机数的终止值
private int end;
//构造函数
public RandomInMap(int start,int end){
this.start=start;
this.end=end;
}
public RandomInMap(int end){
this.end=end;
}
//产生随机数的函数,参数为要产生的个数
private void createRandom(int count){
Random r=new Random();
for(int i=0;i
int temp=r.nextInt(this.end);
Integer times=map.get(temp);
if(times == null){
this.map.put(temp,1);
}else{
this.map.put(temp, times+1);
}
}
}
//按序打印随机序列
public void printSequence(int count){
this.createRandom(count);
Set set=this.map.keySet();
for(int iter:set){
int times=this.map.get(iter);
for(int i=0;i
System.out.println(iter);
}
}
}
//测试代码
public static void main(String[] args) {
new RandomInMap(100).printSequence(101);
}
}
2011年4月06日 09:39
893
0
0
0