private
static
int
getRandom(
int
count)
{
return
(
int
)
Math.round(Math.random() * (count));
}
private
static
String
string =
"abcdefghijklmnopqrstuvwxyz"
;
private
static
String
getRandomString(
int
length){
StringBuffer
sb =
new
StringBuffer();
int
len
= string.length();
for
(
int
i
=
0
;
i < length; i++) {
sb.append(string.charAt(getRandom(len-
1
)));
}
return
sb.toString();
}
/**
*随即生成10以内的字符串 并排序 的核心代码
*/
public void testSort3(){ List<String> stringList = new ArrayList<String>();
String randomStr = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
//输入10以内随机数
Random random = new Random();
for(int i = 0; i < 10;i++){
StringBuilder builStr = new StringBuilder();
int sort = random.nextInt(10)+1;
//random生成的是类似[0,1),左包含又不包含的数,所以要+1
for(int j = 0;j < sort;j++){
char tempC = randomStr.charAt(random.nextInt(randomStr.length()));
builStr.insert(j, tempC);
}
String tempStr = builStr.toString();
stringList.add(tempStr);
}
System.out.println("-------排序前-------");
for(String string:stringList){
System.out.println("元素:"+string);
}
Collections.sort(stringList);
System.out.println("-------排序后-------");
for(String string:stringList){
System.out.println("元素:"+string);
}
}
String randomStr = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
//输入10以内随机数
Random random = new Random();
for(int i = 0; i < 10;i++){
StringBuilder builStr = new StringBuilder();
int sort = random.nextInt(10)+1;
//random生成的是类似[0,1),左包含又不包含的数,所以要+1
for(int j = 0;j < sort;j++){
char tempC = randomStr.charAt(random.nextInt(randomStr.length()));
builStr.insert(j, tempC);
}
String tempStr = builStr.toString();
stringList.add(tempStr);
}
System.out.println("-------排序前-------");
for(String string:stringList){
System.out.println("元素:"+string);
}
Collections.sort(stringList);
System.out.println("-------排序后-------");
for(String string:stringList){
System.out.println("元素:"+string);
}
}