为了账号安全,请及时绑定邮箱和手机立即绑定

6-5交作业了

package com.imooc.collectionandmap;

import org.apache.commons.lang3.RandomStringUtils;

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

/**
 * 练习6-5:利用Collections.sort()方法对泛型为String的List进行排序升级
 * 1、创建完List<String>之后,往其中添加十条随机字符串
 * 2、每条字符串的长度为10以内的随机整数
 * 3、每条字符串的每个字符都为随机生成的字符,字符可以重复
 * 4、每条随机字符串不可重复
 */

public class StudentTestSort {

    public void testSort() {
        List<String> list = new ArrayList<String>();
        Random random = new Random();
        Integer k;
        String stb;
        for (int i = 0; i < 10; i++) {
            do {
                k = random.nextInt(10);
                stb = RandomStringUtils.randomAlphanumeric(k);
            }while (list.contains(stb) || stb == "");
            list.add(stb);
            System.out.println("成功添加字符串" + (i+1) + ":" + stb);
        }
        for (String st : list) {
            System.out.println("元素为:" + st);
        }
    }

    public static void main(String[] args) {
        StudentTestSort sort = new StudentTestSort();
        sort.testSort();
    }
}


正在回答

举报

0/150
提交
取消
Java入门第三季
  • 参与学习       409778    人
  • 解答问题       4339    个

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

进入课程

6-5交作业了

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信