ITPub博客

首页 > IT职业 > IT生活 > 关于自动生成字符串......

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

IT生活 作者:karson 时间:2021-09-09 09:21:50 0 删除 编辑

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 str = new ArrayList();

     for(int i = 0;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

              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 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();

         }

    }


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1542/viewspace-2802674/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: PHP 简单留言板
请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    81
  • 访问量
    660