手记

我在Java第三季的小练习

import java.util.*;

public class test {

    public static void main(String[] args) {
        test  ts = new test();
        ts.testSort2();
    }
    public void testSort2() {
        List<String> stringList = new ArrayList<String>();
        String c = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        for (int i = 0; i < 10; i++) {
            StringBuffer str = new StringBuffer();
            do {
                Random random = new Random();
                int suiji = random.nextInt(10)+1;

                for(int j = 0; j < suiji; j++){
                    int suiji2 = random.nextInt(c.length());
                    str.append(c.charAt(suiji2));
                }

            } while (stringList.contains(str.toString()));
            stringList.add(str.toString());
            System.out.println("成功添加字符串:"+ str.toString());
        }

        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);
        }

    }

}
3人推荐
随时随地看视频
慕课网APP