首页 > IT职业 > IT生活 > 关于自动生成字符串......
package com.imooc.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsTest {
public void strSort() {
List
for(int i = 0;i
String s = getStr(count());
do {
if(!(str.contains(s))) {
str.add(s);
System.out.println("成功添加字符:" + s);
}
}while(s == "");
}
System.out.println("排序前----------------");
fore(str);
Collections.sort(str);
System.out.println("排序后----------------");
fore(str);
}
public String getStr(int length) {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for(int i = 0;i
int number = random.nextInt(62);
sb.append(str.charAt(number));
}
return sb.toString();
}
public int count() {
Random random = new Random();
int count = 0;
do {
count = random.nextInt(10);
}while(count == 0);
return count;
}
public void fore(List
for(String s:x) {
System.out.println("元素:" + s);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CollectionsTest ct = new CollectionsTest();
ct.strSort();
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1542/viewspace-2802674/ ,如需转载,请注明出处,否则将追究法律责任。