手记

JAVA入门第三章 6-5课后作业 随机字符串排序

package stringsorttest;

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

public class StringSortTest {

    public static void main(String[] args) {
        List<String> stringList = new ArrayList<String>();
        Random rdm = new Random();
        String a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        for(int j = 0;j < 10;j++){
            StringBuilder str = new StringBuilder();
            do {
                int length = rdm.nextInt(10)+1;
                for(int i = 0;i < length;i++ ){
                    str.append(a.charAt(rdm.nextInt(a.length())));
                }

            } while (stringList.contains(str.toString()));
            stringList.add(str.toString());
        }
        System.out.println("------添加到集合中的元素:------");
        for (String string : stringList) {
            System.out.println(string);
        }
        System.out.println("------排序后集合中的元素:------");
        Collections.sort(stringList);
        for (String string : stringList) {
            System.out.println(string);
        }
    }
}
2人推荐
随时随地看视频
慕课网APP