//使用StringBuilder
package com.imooc.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class TestSort {
//方法
public void testRandomString() {
List<String> stringList=new ArrayList<String>();
Random random=new Random();
String base="0123456789"
+ "abcdefghijklmnopqrstuvwxyz"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int stringlen=base.length();
int len;
for(int i=0;i<10;i++) {//每条字符串
String str1=null;//每条字符串
StringBuilder strBu=new StringBuilder();//每条字符串的StringBulider形式
do {
len=random.nextInt(10);
int k;//每个字符在st中的索引
for(int j=0;j<len;j++) {//每个字符
k=random.nextInt(stringlen);
strBu.append(base.charAt(k));
}
str1=strBu.toString();
}while(stringList.contains(str1)||len==0);
stringList.add(str1);
}
System.out.println("------排序前--------");
for (String string : stringList) {
System.out.println(string);
}
System.out.println("------排序后--------");
Collections.sort(stringList);
for (String string : stringList) {
System.out.println(string);
}
}
public static void main(String[] args) {
TestSort ts=new TestSort();
ts.testRandomString();
}
}
打开App,阅读手记