写了一个小作业的demo:生成5-10位的随机字符串,并且字符串内无重复字符!!!排序后,还真是按数字,大写,小写的顺序排列。
public void createListStr() {
String str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int len = str.length();
for (int i = 0; i < 10; i++) {
int itemLen = random.nextInt(6) + 5; // 5-10包括5和10
int start, end;
String resStr = "";
for (int k = 0; k < itemLen; k++) {
String item;
do {
start = random.nextInt(len - 1);
end = start + 1;
item = str.substring(start, end);
} while (resStr.indexOf(item) > -1);
resStr += item;
}
listStr.add(resStr);
}
}
public void showListStr() {
System.out.println(listStr.toString());
}
仅供参考学习与交流!!!