package?hyhy_collection_map_demo;
/**
?**
?*?1?創(chuàng)建?List<String>?泛型,添加十條隨機字符串;
?*?2?每條字符的長度為10以內的隨機整數;
?*?3?每條字符串的每個字符都為隨機生成的字符,字符可以重復;
?*?4?每條隨機字符串不可以重復。
?*/
import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.List;
import?java.util.Random;
public?class?SortPractice?{
public?List<String>?listSort;
public?void?listBorn()?{
this.listSort?=?new?ArrayList<String>();
String?base?=?"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random?random?=?new?Random(48);
String?sb1;
for?(int?i?=?0;?i?<?10;?i++)?{
do?{
StringBuilder?sb?=?new?StringBuilder();
Integer?k?=?random.nextInt(10)?+?1;
for?(int?j?=?0;?j?<?k;?j++)?{
sb.append(base.charAt(random.nextInt(base.length())));
}
sb1?=?sb.toString();
}?while?(listSort.contains(sb1));
listSort.add(sb1);
System.out.println("成功添加字符串:"?+?sb1);
}
System.out.println("---------排序前---------");
for?(String?sb?:?listSort)?{
System.out.println("字符串:"?+?sb);
}
System.out.println("---------排序后----------");
Collections.sort(listSort);
for?(String?sb?:?listSort)?{
System.out.println("字符串:"?+?sb);
}
}
public?static?void?main(String[]?args)?{
SortPractice?sp?=?new?SortPractice();
sp.listBorn();
}
}
2015-12-28
沒問題啊