import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; /** * 创 建 人: 巫君丽 * 创建时间: 2019/07/19 * 项目名称: ACTestPlatfrom * 包 名: com.ac.qa.userTest.imoco.test */ public class collectionTest { public void testSort3() { //通过collection.sort方法对String泛型的排序 //添加10条随机字符串 //长度为10以内随机数 //每条字符串的每个字母为随机,可以重复 //字符串不可以重复 String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; Random random = new Random(); List<String> stringlist = new ArrayList<>(); StringBuilder sb = null; for (int i = 0; i < 10; i++) { do{ sb = new StringBuilder(); int length = random.nextInt(10); for (int j = 0; j < length; j++) { int number = random.nextInt(str.length()); sb.append(str.charAt(number)); } }while (stringlist.contains(String.valueOf(sb))); stringlist.add(String.valueOf(sb)); } System.out.println("------------------排序前-----------------------------"); for (String s : stringlist) { System.out.println("元素"+s); } Collections.sort(stringlist); System.out.println("------------------排序后-----------------------------"); for (String s : stringlist) { System.out.println("元素"+s); } } public static void main(String[] args) { collectionTest ct =new collectionTest(); ct.testSort3(); } }
???,厉害了