package com.imooc.course;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class TS {
List <String>list =new ArrayList<String>();
Random random=new Random();
public String getRandomString(int length) {
//定义一个字符串(A-Z,a-z,0-9)即62位;
String str="zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
//由Random生成随机数
Random random=new Random();
StringBuffer sb=new StringBuffer();
//长度为几就循环几次
for(int i=0; i<length; ++i){
//产生0-61的数字
int number=random.nextInt(62);
//将产生的数字通过length次承载到sb中
sb.append(str.charAt(number));
}
//将承载的字符转换成字符串
return sb.toString();
}
public void addlist() {
String i ;
int length;
for(int j=0;j<10;j++) {
do {
length=random.nextInt(10);
} while (length==0);
do{i = getRandomString(length);
}
while (list.contains(i));
{list.add(i);}
}
System.out.println("list的元素依次是:");
for (String string :list ) {
System.out.print(string+" ");
}
System.out.println(" ");
Collections.sort(list);
System.out.println("排序后list的元素依次是:");
for (String string :list ) {
System.out.print(string+" ");
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
new TS().addlist();
}
}
运行了一下你的代码,感觉没啥问题
这个感觉不是随机