package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class HelloWorld {
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
final int fixedSize=10;
int count=0;
while(count!=fixedSize){
char[] strs=new char[10];
// System.out.println(x+"th string:");
Random rand=new Random();
int length=rand.nextInt(10)+1;
// System.out.println("length:"+length);
for(int i =0; i<length; i++){
int flag=rand.nextInt(3);
// System.out.println("flag:"+flag);
switch(flag)
{
case 0://numbers
strs[i]=(char) ('0'+rand.nextInt(10));
// System.out.println(i+"th char is "+strs[i]);
break;
case 1://lowercase letters
strs[i]=(char) ('a'+rand.nextInt(26));
// System.out.println(i+"th char is "+strs[i]);
break;
case 2://Uppercase letters
strs[i]=(char) ('A'+rand.nextInt(26));
// System.out.println(i+"th char is "+strs[i]);
break;
}
}
String toAdd=new String(strs);
// System.out.println(toAdd);
// System.out.println("--------------------------------------");
if(list.contains(toAdd)){
continue;
}else{
list.add(toAdd);
// System.out.println("Successfully added string");
count++;
}
// System.out.println(count);
// do{
// list.add(toAdd);
// }while(!(list.contains(toAdd)));
}
System.out.println(list.toString());
System.out.println("-------排序前--------------------");
for (String a : list) {
System.out.println("元素: "+a);
}
Collections.sort(list);
System.out.println("-------排序后--------------------");
for (String a : list) {
System.out.println("元素: "+a);
}
}
}
打开App,阅读手记
热门评论
取到每个字符的概率不等吧..