package imooc; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; public class cTest { public void sortTest(){ List<String> list=new ArrayList<String>(); Random random=new Random(); for(int i=0;i<10;i++)//十条字符串 { StringBuilder string=new StringBuilder(); do {int k=random.nextInt(9)+1;//随机生成长度,防止生成空字符串 for(int n=0;n<k;n++)//往字符串里随机添加字符 { int c=random.nextInt(10); string.append(c); } } while(list.contains(string)); list.add(string.toString()); } //打印 System.out.println("----------排序前----------"); for(String a:list){ System.out.println(a); } Collections.sort(list);//排序 System.out.println(""); //打印 System.out.println("----------排序后----------"); for(String a:list){ System.out.println(a); } } public static void main(String[] args) { // TODO Auto-generated method stub cTest test=new cTest(); test.sortTest(); } }
随机字符串的话,可以参照ASCII码