/**
* 2. 对String泛型的List进行排序
* 1)创建String泛型的List,往其中添加十条随机字符串
* 2)每条字符串的长度为10以内的随机整数
* 3)每条字符串的每个字符都为随机生成的字符,字符可以重复
* 4)每套随机字符串不可重复
* 调用sort方法,再次输出排序后的顺序
*/
public void testSort21(){
List<String> stringList1 = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
StringBuilder sb = new StringBuilder();
Random random = new Random();
do{
String s = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int length = random.nextInt(10);
for (int j = 0; j <= length; j++){
sb.append(s.charAt(random.nextInt(62)));
}
} while (stringList1.contains(sb.toString()));
stringList1.add(sb.toString());
}
System.out.println("--------------排序前-----------------");
for (String string : stringList1){
System.out.println("元素: " + string);
}
Collections.sort(stringList1);
System.out.println("--------------排序后-----------------");
for (String string : stringList1){
System.out.println("元素: " + string);
}
}
點(diǎn)擊查看更多內(nèi)容
1人點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫(xiě)下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦