Collections.sort()问题
调用Collections.sort()方法,为什么sort会报错?
是创建Stringbuilder的原因吗
package com.imooc.collection;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Collections {
public void testsort1(){
//给List中添加泛型
List <String> stringlist=new ArrayList <String> ();
//创建字符
String str="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
//创建Random对象
Random random=new Random();
for(int i=0;i<10;i++){
//c创建一个String类型的变量K
StringBuilder sb=new StringBuilder();
for(int j=0;j<1+random.nextInt(10);j++){
char cr=str.charAt((int)(random.nextInt(62)));
sb=sb.append(String.valueOf(cr));
System.out.println("获取的的字符串:"+sb);
stringlist.add(sb.toString());
}
}
System.out.println("----排序前----");
System.out.println("公共:"+stringlist.size()+"个元素");
for (String string : stringlist) {
System.out.println("排序前元素:"+string);
}
System.out.println("---排序后---");
System.out.println("公共:"+stringlist.size()+"个元素");
Collections.sort(stringlist);
for (String string : stringlist) {
System.out.println("排序后:"+string);
}
}
public static void main(String[] args){
Collections it=new Collections();
it.testsort1();
}
}