1.创建完List<String>之后,往其中添加十条随机字符串
2.每条字符串的长度为10以内的随机整数
3.每条字符串的每个字符都为随机生成的字符,字符可以重复
4.每条随机字符串不可重复
package com.imooc.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class StringCollectionsSort {
public void testSort(){
String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
List<String> stringList=new ArrayList<String>();
Random random=new Random();
for(int i=0;i<10;i++){
StringBuilder sb = new StringBuilder();
int j=random.nextInt(9)+1;
do{
for(int k=0;k<j;k++){
int m = random.nextInt(base.length());
sb.append(base.charAt(m));
}
}while(stringList.contains(sb));
stringList.add(sb.toString());
System.out.println("成功添加字符串:"+sb);
}
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);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
StringCollectionsSort gs=new StringCollectionsSort();
gs.testSort();
}
}