第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

Java入門第三季_Collections隨機字符串排序

標簽:
Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsTest {
/**
     * 返回随机字符a-z,A-Z,0-9
     */
    public char getRandomCharacter() {
        Random random = new Random();
        int number = random.nextInt(3);
        char result = ' ';
        //随机生成字符的类型
        switch (number) {
            case 0:
                result = (char) ((int) (Math.random() * 26 + 65));

                break;
            case 1:
                result = (char) ((int) (Math.random() * 26 + 97));

                break;
            case 2:
                result = (char) ((int) (Math.random() * 10 + 48));

                break;
            default:
                getRandomCharacter();
        }
        return result;
    }
/**
     * 1.创建完List<String>后,往其中添加10条随机字符串
     * 2.每条字符串长度为10以内的整数
     * 3.每条字符串的每个字符随机生成且可以重复
     * 4.但是每条字符串都不相同
     */
    public void testStringCollectionsSort2() {
        List<String> stringList = new ArrayList<String>();
        Random random = new Random();
        //生成10条随机字符串
        for (int i = 0; i < 10; i++) {
            //每条字符串长度10以内
            int length = random.nextInt(10);
            //初始的字符串
            String randomString = " ";
            do {
                //往初始字符串里添加字符
                while (length > 0) {
                    randomString += getRandomCharacter();
                    length--;
                }
            } while (stringList.contains(randomString));
            stringList.add(randomString);

        }
        System.out.println("-------排序前-------");
        for (String string : stringList) {
            System.out.println("随机字符串:" + string);
        }
        Collections.sort(stringList);
        System.out.println("-------排序后-------");
        for (String string : stringList) {
            System.out.println("随机字符串:" + string);
        }
    }
public static void main(String[] args) {
        CollectionsTest ct = new CollectionsTest();
        //ct.testIntegerCollectionsSort();
        //ct.testStringCollectionsSort1();
        ct.testStringCollectionsSort2();
        //System.out.println(ct.getRandomCharacter());
    }
}
點擊查看更多內容
2人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質文章

正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優(yōu)惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消