贴一下自己代码,看有木有要改进的地方
package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsTest {
public void testRandomStringSort(){
String az09 = "0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
List<String> stringList = new ArrayList<String>();
Random rd= new Random();
for(int i=0;i<10;i++){
int strLength = rd.nextInt(9)+1;
String strRandom = "";
do{
strRandom = "";
for(int j=0;j<strLength;j++){
int num = rd.nextInt(az09.length());
strRandom += az09.charAt(num);
}
}while(stringList.contains(strRandom));
stringList.add(strRandom);
System.out.println("成功添加:"+strRandom);
}
System.out.println("——————排序前————————");
Collections.sort(stringList);
for(String str:stringList){
System.out.println("元素:"+str);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CollectionsTest clt = new CollectionsTest();
clt.testRandomStringSort();
}
}