没茅台
2016-11-07 15:18:31浏览 3272
package cn.edu.lin.task;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* Created by asus on 2016/11/4.
*/
public class CollectionsTask {
public static List<String> listRandomString() {
String str = "qwertyuiopasdfghjklzxcvbnmQWERRTYUIOPASDFGHJKLZXCVBNM1234567890";
Random random = new Random();
List<String> listString = new ArrayList<String>();
String[] strArray = new String[10];
for (int i = 0; i < 10; i++) {
StringBuffer stringBuffer = new StringBuffer();
//确定字符串长度
int size = (int) (Math.random() * 10);
for (int j = 0; j < size; j++) {
int index = random.nextInt(str.length());
char c = str.charAt(index);
stringBuffer.append(c);
}
if (!(listString.contains(stringBuffer.toString()))) {
listString.add(stringBuffer.toString());
} else {
i--;
}
}
return listString;
}
public static void main(String[] args){
List<String > strList = listRandomString();
System.out.println("--------生成10条随机字符串--------");
for(String string : strList){
System.out.println(string);
}
System.out.println("--------排序后-------------");
Collections.sort(strList);
for(String string :strList){
System.out.println(string);
}
}
}
热门评论
发错人了 可以忽略那些东西
public void sorttext3(){
List<String> a = new ArrayList<String>();
//String[] b = new String[10];
String st = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random c = new Random();
for(int z=0;z<10;z++){
StringBuffer sb = new StringBuffer();
do{
int j=c.nextInt(10)+1;
for(int i=0;i<j;i++){
//Random d = new Random(62);
int num = c.nextInt(st.length());
sb.append(st.charAt(num));
}
}while(a.contains(sb.toString()));
a.add(sb.toString());
}
把你的StringBuffer 对象放到了for循环里了 ,要不每次循环,都会把之前循环过了字符串加里面,还有就是数组越界,不清楚怎么回事,不过改的代码,是没问题的!
String[] strArray=new String[10]; 这句没用吧,注释掉,结果也是正确的!