继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

java入门第三季_6-5作业

qq_常虎_0
关注TA
已关注
手记 4
粉丝 0
获赞 2

package imooc_collection_map_demo;

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

public class CollectionsTest {

public static final String allchar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

public void testSort2(){
    List<String> stringList = new ArrayList<String>();
    String k;
    for(int i = 0; i < 10; i++){
        do{
            k = testString();
        }while(stringList.contains(k));
        stringList.add(k);
        System.out.println("将要添加的字符串:" + stringList.get(i));
    }
    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 String testString(){
    StringBuffer sb = new StringBuffer();
    Random random = new Random();
    int j = random.nextInt(10) + 1;
    for(int i = 0; i < j; i++){
        sb.append(allchar.charAt(random.nextInt(allchar.length())));
    }
    return sb.toString();
}

public static void main(String[] args) {

    CollectionsTest ct = new CollectionsTest();
    ct.testSort2();
}

}

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP