package com.imooc.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class collectiontest {
//随机生成字符串
public static String getRandomString(int length) {
//length表示生成字符串的长度
String base = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String b = null;
List<String> a=new ArrayList<String>();
Random random=new Random();
collectiontest ct=new collectiontest();
for(int i=0;i<10;i++)
{Integer k=random.nextInt(10);
do{ b=collectiontest.getRandomString(k);}while(a.contains(b));
a.add(b);}
Collections.sort(a);
System.out.println("排序后");
for(String c:a){System.out.println("元素"+c);}}
}
打开App,阅读手记