java生成随机字符串有空的,望大大指导一下

Tsbasa 2016-08-31 07:07:02
生成随机长度的随机字符串
遍历出来有空的
望大神指导一下,怎么写才行

List<String> stringList = new ArrayList<String>();
String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random random = new Random();
for (int i = 0; i < 10; i++) {
StringBuffer str = new StringBuffer();
do {
for (int j = 0; j < random.nextInt(10); j++) {
int num = random.nextInt(base.length());
str.append(base.charAt(num));
}
} while (stringList.contains(str.toString()) && str.length() < 1);
stringList.add(str.toString());
}
...全文
118 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tsbasa 2016-08-31
  • 打赏
  • 举报
回复
引用 2 楼 rickylin86 的回复:
不知道你是不是照书抄写的代码. 你上面的代码应该改成如下这样就不会出现空的结果了

} while (stringList.contains(str.toString()) || str.length() < 1);
感谢感谢
rickylin86 2016-08-31
  • 打赏
  • 举报
回复
不知道你是不是照书抄写的代码. 你上面的代码应该改成如下这样就不会出现空的结果了

} while (stringList.contains(str.toString()) || str.length() < 1);
rickylin86 2016-08-31
  • 打赏
  • 举报
回复

j < random.nextInt(10)
for循环中如果random.nextInt(10) = 0的话那么对应的StringBuffer.toString()就是空的字符串.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧