MKdd
2016-09-14 13:33:05浏览 3176
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class sortString {
public void testSort2_0(){
List<String> st = new ArrayList<String>();
Random random =new Random();
String s="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < 10; i++) {
StringBuilder temp = new StringBuilder("");//存储临时产生的字符串
do{
int k = random.nextInt(9)+1;//保存随机字符串的长度
for (int j = 0; j < k; j++) {
int a = random.nextInt(s.length());//从s中取出一个随机字符
temp.append(s.charAt(a));//将取出的字符添加到存储临时字符串的末尾
}
System.out.println("将要添加的字符串" + "'"+temp+"'");
}while(st.contains(temp.toString()));//判断得到的字符串是否重复
st.add(temp.toString()); //重复继续循环取字符串,不重复则将其添加到list中
}
System.out.println("-------排序前-------------");
for (String string : st) {
System.out.println("字符串为:"+ string);
}
Collections.sort(st);//对list排序
System.out.println("-------排序后-------------");
for (String string : st) {
System.out.println("字符串为:"+ string);
}
}
public static void main(String[] args) {
sortString test = new sortString();
test.testSort2_0();
}
}