package com.imooc.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Test {
public String s = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public int len = s.length();
public List<String> stringList = new ArrayList<String>();
public void stringRandom(){
for(int k=0;k<10;k++){
String str = "";
Random random = new Random();
int c;
//获取10以内的随机整数,遇到0则重新获取
do{
c=random.nextInt(10);
}while(c==0);
//取随机字符,组成字符串
for(int i=0;i<c;i++){
int r = random.nextInt(61);
String temp = s.substring(r, r+1);
str = str+temp;
}
//判断字符串是否已经存在,如不存在则插入
if(!stringList.contains(str)){
stringList.add(str);
System.out.println("插入字符串:"+str);
}
}
System.out.println("---------排序前--------");
//使用foreach进行输出各元素
for(String string:stringList){
System.out.println("元素:"+string);
}
System.out.println("---------排序后--------");
//使用Collections的sort方法进行排序
Collections.sort(stringList);
for(String string:stringList){
System.out.println("元素:"+string);
}
}
public static void main(String[] args) {
Test t = new Test();
t.stringRandom();
}
}