package collection_set_demo; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; public class CollectionsTest { public void testSort2(){ List<String> stringList = new ArrayList<String>(); Random random = new Random(); String source = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; for(int i = 0; i < 10; i++){ String string = ""; do{ int length = random.nextInt(11); for(int j = 0; j < length; j++){ char x = source.charAt(random.nextInt(source.length())); string = string + x; } }while(stringList.contains(string)); stringList.add(string); System.out.println("成功添加字符串:" + string + "\t\t" + ",字符串长度:" + string.length()); } System.out.println("----------------排序前---------------"); for (String string1 : stringList) { System.out.println(string1); } Collections.sort(stringList); System.out.println("----------------排序后---------------"); for (String string1 : stringList) { System.out.println(string1); } } public static void main(String[] args) { // TODO Auto-generated method stub CollectionsTest ct = new CollectionsTest(); ct.testSort2(); } }
写的很好
可以可以