手记

6-5的解答

package randomlist;

import java.util.ArrayList;
import java.util.Arrays;

import java.util.Collections;
import java.util.List;

public class test {
public List<String>randomlist=new ArrayList<String>();

public String random(){
    StringBuilder arraylist=new StringBuilder();
    //因为stringbuilder是可变型,所以不用设定长度,添加即可

        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        int j=(int)(Math.random()*10);
        do{
        //StringBuilder arraylist=new StringBuilder();

        for(int i=0;i<j;i++){arraylist.append(str.charAt((int)(Math.random()*str.length()))); }
        System.out.println("将要添加的字符串"+arraylist);
        return arraylist.toString();}while(j==0);
        }

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    test t=new test();
    for(int i=0;i<10;i++){

    String j=t.random();
    //for(String s:t.randomlist){

    //if(j.equals(s)){j=t.random();}

    //else

    t.randomlist.add(j);}
    System.out.println("------------排序前-------------");
    for(String s:t.randomlist){
        System.out.println("集合中的元素"+s);}
    System.out.println("------------排序后-------------");
         Collections.sort(t.randomlist);
         for(String g:t.randomlist){
             System.out.println("集合中的元素"+g);} 
         }

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