我的作业,,
public void testSort3() {
List<String> stringList =new ArrayList<String>();
String k;
for(int i = 0; i< 10; i++) {
do {
k = generateString(10);
}while(stringList.contains(k));
stringList.add(k);
}
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);
}
}
/**
* 返回随机字符串方法
* @param args
*/
public static String generateString(int length) //参数为返回随机数的长度
{
StringBuffer sb = new StringBuffer();
Random random = new Random();
Random random2 =new Random();
int j;
j= random2.nextInt(length);
for (int i = 0; i < j; i++)
{
j= random2.nextInt(length);
sb.append(allChar.charAt(random.nextInt(allChar.length())));
}
return sb.toString();
}