手记

阿圭罗安弟_JAVA入门第三季_随机字符串排序

输入代码

public void testSort3(){
List<String> randStringList = new ArrayList<String>();
String str = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789";
Random random = new Random();
String k;
int n;

    for(int j = 0; j < 10; j ++){
        StringBuffer sb = new StringBuffer();  //StringBuffer类生成,为了拼接字符串
        do{
            n = random.nextInt(10);
        }while(n<=0);
        for(int i= 0; i < n; i++){

            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        do{
            k = sb.toString();
        }while(randStringList.contains(k));
        randStringList.add(k);
        System.out.println("成功添加元素"+k);
    }
    System.out.println("-------排序前----------");
    for (String string : randStringList) {
        System.out.println("元素"+string);
    }

    Collections.sort(randStringList);
    System.out.println("-------排序后----------");
    for (String string : randStringList) {
        System.out.println("元素"+string);
    }

}

public static void main(String[] args) {
CollectionTest ct = new CollectionTest();
ct.testSort3();
}

1人推荐
随时随地看视频
慕课网APP