public void testSort3(){
/**
- 创建10条随机字符串
- 字符串的长度在10以内
- 字符可随机生成,可重复
-
10条字符串不重复
*/
List<String> stringList = new ArrayList<String>();String a = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; Random random = new Random(); int count=0; while(count<10){ StringBuilder stringbuilder = new StringBuilder(); int rand = random.nextInt(10); for (int i = 0; i <= rand; i++) { int rand1 = (int) (Math.random() * a.length()); char c = a.charAt(rand1); stringbuilder = stringbuilder.append(c); } if(!stringList.contains( stringbuilder)){ stringList.add(stringbuilder.toString()); count++; System.out.println("字符串:"+stringbuilder); } } 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); }
}