collections.sort不太理解
collections.sort只能对list进行排序,对set不行吗?
collections.sort只能对list进行排序,对set不行吗?
2015-09-28
public static Random ran = new Random();
public static List<Character> chars = new ArrayList<Character>();
static{
for(int i=48;i<=57;i++){
chars.add((char)i);
}
for(int i=65;i<=90;i++){
chars.add((char)i);
}
for(int i=97;i<=122;i++){
chars.add((char)i);
}
System.out.println("随机数字及字符串组成的数组:");
System.out.println(Arrays.toString(chars.toArray()));
System.out.println("========================");
}
public static char getRandomchar(){
int index = ran.nextInt(chars.size());
return chars.get(index);
}
public static String generateRandomStr(int length){
String str = "";
for(int i=0;i<length;i++){
str+=getRandomchar();
}
return str;
}
public static List<String> generateRandomStrs(int num){
List<String> list = new ArrayList<String>();
int i=0;
while(true){
String str = generateRandomStr(10);
if(list.contains(str)){
continue;
}
list.add(str);
i++;
if(i == num){
break;
}
}
return list;
}
public static void main(String[] args) {
System.out.println("=============排序前=================");
List<String> result = generateRandomStrs(10);
for (String str : result) {
System.out.println("输出随机数组:"+str);
}
System.out.println("=============排序后=================");
Collections.sort(result);
for (String str : result) {
System.out.println("输出随机数组:"+str);
}
}
举报