手记

java第三季6-5练习添加随机字符串

package com.imooc;

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

public class StringSort {
List <String> one;
public StringSort(){
one=new ArrayList<String>();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
/ 1创建完List<String>之后,往其中添加十条随机字符串
2每条字符串长度为10以内的随机字符串
3每条字符串都为随机生成的字符,字符可以重复
4每条随机字符串不可重复
/
Random A=new Random();
List<String> one=new ArrayList<String>();
String B="123456789ABCDEFGHIGK";
String string;

    for(int i=0;i<10;i++){
         StringBuilder C=new StringBuilder();
         do{
            int D=A.nextInt(B.length()-1);
            int E=A.nextInt(10)+1;

            for(int ii=0;ii<E;ii++){
                if(D==0)
                    D=A.nextInt(B.length()-1);
                C.append(B.charAt(A.nextInt(D)));}
                string =C.toString();

         }while(one.contains(string));
         System.out.println("成功添加第"+(i+1)+"个元素:"+string);
         one.add(string);

    }
     System.out.println("----------排序前------------");
     for(String one1:one){
         System.out.println(one.indexOf(one1)
                 +one1);
     }
    System.out.println("-----------排序后-----------------"); 
    Collections.sort(one);
    for(String one1:one){
     System.out.println(one.indexOf(one1)
             +one1);
    }

    }}

        **加粗文字**
1人推荐
随时随地看视频
慕课网APP