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

关于生成随机字符串的问题

我是叶霖
关注TA
已关注
手记 2
粉丝 3
获赞 4

package com.imooc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class CollectionsSort {

public void testSort3() {
    List<String>stringlist = new ArrayList<String>();
    String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  //定义了一个字符创 作为随机生成字符串的的字符范围
    Random random = new Random();

    for(int i = 0; i<10;i++) {
        StringBuffer sb = new StringBuffer();   //实例化一个对象 用来存放String 类型的字符串
         for(int j = 0;j<random.nextInt(10)+1;j++) {
             do {
        int a = random.nextInt(base.length());
        sb.append(base.charAt(a));
             }while(stringlist.contains(sb));
         }
         stringlist.add(sb.toString());
         System.out .println("随机生成字符串"+sb);
    }
    System.out.println("------------------排序前----------------------");
    for (String string : stringlist) {
        System.out.println("String类的是:"+string);
    }
    Collections.sort(stringlist);
    System.out.println("------------------排序后----------------------");
    for (String string : stringlist) {
        System.out.println("String类是:"+string);
    }
}

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

}

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

热门评论

你这个是不是和题目有点反了,题目说 十组字符串,一组和一组是不能重复的,但是每组中的每个字符之间可以重复,你这个出来之后,一组和一组可以重复,而每个字符之间不能重复了

查看全部评论