手记

java第三季第六章6-5课后练习

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

public class Exec01 {

    /*
     * 生成十个不同的字符串,并输出和排序
     */
    public void strSort() {
        String strBase = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        List<String> strList = new ArrayList<String>();
        Random ran = new Random();
        for (int i = 0; i < 10; i++) {
            String str1 = new String();
            do {
                int size = ran.nextInt(10)+1;
                for (int m = 0; m < size; m++) {
                    int offset = ran.nextInt(62);
                    str1 += strBase.charAt(offset);
                }
            } while (strList.contains(str1));
            System.out.println("将要添加字符串:"+str1);
            strList.add(str1);
        }
        System.out.println("--------排序前--------");
        for (String string : strList) {
            System.out.println("元素:"+":"+string);
        }
        Collections.sort(strList);
        System.out.println("--------排序后--------");
        for (String string : strList) {
            System.out.println("元素:"+":"+string);
        }
    }

    public static void main(String[] args) {
        Exec01 ec = new Exec01();
        ec.strSort();
    }
}
5人推荐
随时随地看视频
慕课网APP

热门评论

我先来mark一下下~


查看全部评论