手记

java入门第三季_6-5作业

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

}

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