看了众多幕友的代码,实在脑洞大开,牛到爆炸,还是要多学习啊....
思考了许久,贴上核心代码希望能跟大家一起交流!
public void testSort3(){
List<String> stringList=new ArrayList<String>();
String st="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Random random=new Random();
for(int k=1;k<=10;k++){
StringBuilder zy= new StringBuilder();
int j=random.nextInt(10)+1;
do {
for(int i=1;i<=j;i++){
zy.append(st.charAt(random.nextInt(st.length())));
}
}while(stringList.contains(zy));
stringList.add(zy.toString());
}
System.out.println("------------排序前------------");
for (String string : stringList) {
System.out.println("元素:"+string);
}
Collections.sort(stringList);
System.out.println("------------排序后------------");
for (String string : stringList) {
System.out.println("元素:"+string);
}
}
热门评论
程序是有小问题的,while(stringList.contains(zy));这个判断结果永远是false,可以在stringList.add(zy.toString()); 添加进集合后再判断一次即可看出,System.out.println(stringList.contains(zy));