手记

java入门第三季随机字符串排序

package com.imooc.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionTest {
public void testSort3(){
List<String> stringlist= new ArrayList<String>();
String a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random=new Random();

    for(int j=0;j<10;j++){
        StringBuilder sb=new StringBuilder();
        do{
    for(int i=0;i<random.nextInt(10);i++){

        sb.append(a.charAt(random.nextInt(a.length())));

    }stringlist.add(sb.toString());
    System.out.println("成功添加字符串:"+sb.toString());
    }while (stringlist.contains(sb));

    }

    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);
    }
}
public static void main(String[] args){
    CollectionTest ct=new CollectionTest();
    ct.testSort3();
}

}

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