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

关于自动生成字符串......

慕移动7111842
关注TA
已关注
手记 1
粉丝 0
获赞 1

package com.imooc.collection;


import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Random;


public class CollectionsTest {


     public void strSort() {

     List<String> str = new ArrayList<String>();

     for(int i = 0;i < 10;i++) {

         String s = getStr(count());

         do {

             if(!(str.contains(s))) {

                str.add(s);

                System.out.println("成功添加字符:" + s);

             }

         }while(s == "");

     }

     System.out.println("排序前----------------");

     fore(str);

     Collections.sort(str);

     System.out.println("排序后----------------");

     fore(str);

}

    public String getStr(int length) {

        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

         Random random = new Random();

         StringBuffer sb = new StringBuffer();

         for(int i = 0;i < length;i++) {

              int number = random.nextInt(62);

              sb.append(str.charAt(number));

        }

        return sb.toString();

}

     public int count() {

         Random random = new Random();

         int count = 0;

         do {

              count = random.nextInt(10);

         }while(count == 0);

         return count;

     }

     public void fore(List<String> x) {

         for(String s:x) {

              System.out.println("元素:" + s);

         }

     }

     public static void main(String[] args) {

         // TODO Auto-generated method stub

         CollectionsTest ct = new CollectionsTest();

         ct.strSort();

         }

    }


打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP