public void testSortRandomString(){ List<String> list = new ArrayList<String>(); Integer k; Random random = new Random(); String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < 3; i++) { do { for (int a = 0; a < 10; a++) { k = random.nextInt(str.length()); stringBuilder.append(str.charAt(k)); } }while (list.contains(stringBuilder)); System.out.println(stringBuilder); list.add(stringBuilder.toString()); stringBuilder.delete(0,stringBuilder.length()); } Collections.sort(list); System.out.println("===========排序后============="); for (String i:list) { System.out.println("元素:"+i); } }
public void testSortString2() {
List<String> stringList = new ArrayList<String>();
Integer it;
Random random = new Random();
Random Long = new Random();
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i < 10 ; i++) {
do {
for (int j = 0 ,l=Long.nextInt(10);j<l; j++) {
it = random.nextInt(str.length());
sb.append(str.charAt(it));
}
}while(stringList.contains(sb));
stringList.add(sb.toString());
sb.delete(0, sb.length());
}
System.out.println("---------");
for(String s : stringList) {
System.out.println("元素:"+ s);
}
Collections.sort(stringList);
System.out.println("---------");
for(String s : stringList) {
System.out.println("元素:"+ s);
}
}
改了一下 可以不固定10个字符