至繁归于至简_
2017-07-19 16:47:00浏览 2348
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Test {
private static List<String> stringList;
private static String finalString;
public static void main(String[] args) {
stringList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
do {
createString();
} while (stringList.contains(finalString));
stringList.add(finalString);
}
System.out.println("————————————————————————排序前——————————————————————————");
listOut(stringList);
Collections.sort(stringList);
System.out.println("————————————————————————排序后——————————————————————————");
listOut(stringList);
}
private static void createString() {
Random random = new Random();
StringBuffer sb = new StringBuffer();
int length = random.nextInt(10)+1;
for (int i = 0; i < length; i++) {
int type = random.nextInt(3);
switch (type) {
case 0:
int[] num = {
0,1,2,3,4,5,6,7,8,9,
};
sb.append(num[random.nextInt(10)]);
break;
case 1:
char[] capital = {
'Q','W','E','R','T','U','I','O','P','A','S',
'D','F','G','H','J','K','L','Z','X','C','V',
'B','N','M','Y'
};
sb.append(capital[random.nextInt(26)]);
break;
case 2:
char[] lowercase = {
'q','w','e','r','t','y','u','i','o','p',
'a','s','d','f','g','h','j','k','l','z',
'x','c','v','b','n','m'
};
sb.append(lowercase[random.nextInt(26)]);
break;
default:
break;
}
}
finalString = sb.toString();
}
private static void listOut(List<String> stringList) {
for (String string : stringList) {
System.out.println("元素:"+string);
}
}
}