楚天小虾
2017-03-14 22:50:16浏览 1846
package stringsorttest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class StringSortTest {
public static void main(String[] args) {
List<String> stringList = new ArrayList<String>();
Random rdm = new Random();
String a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for(int j = 0;j < 10;j++){
StringBuilder str = new StringBuilder();
do {
int length = rdm.nextInt(10)+1;
for(int i = 0;i < length;i++ ){
str.append(a.charAt(rdm.nextInt(a.length())));
}
} while (stringList.contains(str.toString()));
stringList.add(str.toString());
}
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);
}
}
}