继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Collections sort()排序方法

leexiaoxiao
关注TA
已关注
手记 1
粉丝 1
获赞 4
public void testSort(){
        List<String> stringList = new ArrayList<String>();
        char[] allchar = {'a','b','c','d','e','f','g','h','i','j','k','l','m',
                'n','o','p','q','r','s','t','u','v','w','x','y','z','0','1',
                '2','3','4','5','6','7','8','9'};
        Random random = new Random();
        for(int i = 0;i<10;i++){
            String k="";
            int x = random.nextInt(10)+1;
            for(int j=0;j<x;j++){
                int z = random.nextInt(35);
                int logn = (int) (Math.random()*10);
                String aChar;
                if(logn<5){
                    aChar = (allchar[z] + "").toUpperCase();
                }
                else{
                    aChar = allchar[z] + "";
                }
                k = k + aChar;
            }
            if(!stringList.contains(k)){
                stringList.add(k);
            }else{
                i--;
            }
        }
        System.out.println("----------排序前----------");
        for(String st:stringList){
            System.out.println(st);
        }
        Collections.sort(stringList);
        System.out.println("----------排序后----------");
        for(String st:stringList){
            System.out.println(st);
        }
    }
打开App,阅读手记
3人推荐
发表评论
随时随地看视频慕课网APP