yayam
2016-10-20 21:05:29浏览 2216
/** * 1、创建完List<String>之后, 往其中添加十条随机字符串 *
* 2每条字符串的长度为10以内的随机整数
* 3、每条字符串的每个字符都为随机生成的字符,字符可以重复
* 4、每条随机字符串不可重复
* */
public void testSort3(){
List<String> stringlistList = new ArrayList<String>();
Random random = new Random();
for(int i = 0; i < 10; i++){
StringBuilder tempString = new StringBuilder();
int ran = random.nextInt(10);
if(ran == 0){
ran = 10;
}
for(int k = 0; k < ran; k++){
int type = random.nextInt(3);
if(type == 1){
tempString.append((char)('0' + random.nextInt(10)));
}
else if(type == 0) {
tempString.append((char)('a' + random.nextInt(26)));
}
else{
tempString.append((char)('a' + random.nextInt(26)));
}
}
if(i == 0)
{
stringlistList.add(tempString.toString());
}
else if(stringlistList.contains(tempString.toString())){
i--;
}
else {
stringlistList.add(tempString.toString());
}
}
System.out.println("----------------排序前------------------");
for(String string : stringlistList){
System.out.println("元素: " + string);
}
Collections.sort(stringlistList);
System.out.println("----------------排序后------------------");
for(String string : stringlistList){
System.out.println("元素: " + string);
}
}
热门评论
int type = random.nextInt(2); 同学,这句代码括号里的数应该改为3吧。即,int type = random.nextInt(3);