日常交作业
List<String> stringList = new ArrayList<String>();
for(int i=0;i<10;i++) {
StringBuilder sb = new StringBuilder(); //用于存储每一个字符串
int length = (int)(Math.random()*10); //字符串长度随机
do {
for(int j=0;j<length;j++) {
int num;
char numChar;
do{
num = (int)(Math.random()*(123-48)+48); // 随机生成[48,122]之间的数值
numChar = (char)num;
}while(!(num>=48 && num<=57 || num>=65 && num<=90 || num>=97 && num<=122)); // 字符'0'-'9','A'-'Z','a'-'z'对应的数
sb.append(numChar);
}
}while(stringList.contains(sb.toString())); //保证字符串不重复
System.out.println("添加第"+(i+1)+"个字符串:"+sb.toString());
stringList.add(sb.toString());
}
System.out.println("-------排序前-------");
for(String string:stringList) {
System.out.println("元素:"+string);
}
Collections.sort(stringList);
System.out.println("-------排序后-------");
for(String string:stringList) {
System.out.println("元素:"+string);
}