package collection_string_sort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Collection_String_Sort {
/**
* 字符串排序
*/
public void stringSort(){
List<String> stringsList = new ArrayList();
String base;
base = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
final int len = base.length();
int length;
int temp;
Random random = new Random();
for(int i = 0;i < 10;i++){
do{
length = random.nextInt(10);
}while(length == 0);
String str = "";
do{
for(int j = 0;j < length;j++){
temp = random.nextInt(len);
str = str + base.charAt(temp);
}
}while(stringsList.contains(str));
System.out.println("添加string "+i+": "+str);
stringsList.add(str);
}
System.out.println("--------------排序前----------------");
temp = 1;
for(String str:stringsList){
System.out.println("String"+temp+": "+str);
temp++;
}
Collections.sort(stringsList);
System.out.println("--------------排序后----------------");
temp = 1;
for(String str:stringsList){
System.out.println("String"+temp+": "+str);
temp++;
}
}
public static void main(String[] args) {
Collection_String_Sort css = new Collection_String_Sort();
css.stringSort();
}
}
打开App,阅读手记