排序方法
/**
* @showball
*1、创建List<String>之后,添加十条随机字符串
*2、每条字符串的长度为10以内的随机整数
*3.每条字符串的每个字符都为随机生成的字符串,字符可以重复
*4.每条随机字符串不可重复
*/
public class CollectionsTest {
public void testSort3(){
List<String>randomStrings=new ArrayList<String>();
Random random=new Random();
String base="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int count=10;
String str;
for (int i = 0; i < count; i++) {
StringBuffer sb=new StringBuffer();
do {
// 每个字符串长度为10以内
for (int j = 0; j < random.nextInt(count); j++) {
int index = random.nextInt(base.length());
sb.append(base.charAt(index));
}
str = sb.toString();
} while (randomStrings.contains(str));
randomStrings.add(str);
}
System.out.println("--------排序前------");
for (String string : randomStrings) {
System.out.println("元素:"+string);
}
System.out.println("------排序后------");
Collections.sort(randomStrings);
for (String string : randomStrings) {
System.out.println("元素:"+string);
}
}
}
主程序
public static void main(String[] args) {
// TODO Auto-generated method stub
CollectionsTest ct=new CollectionsTest();
ct.testSort3();
}
运行结果
--------排序前------
元素:KX370
元素:r
元素:nNjTRG
元素:rHh
元素:Be
元素:UH8
元素:SOA
元素:26
元素:yIP
元素:mPt
------排序后------
元素:26
元素:Be
元素:KX370
元素:SOA
元素:UH8
元素:mPt
元素:nNjTRG
元素:r
元素:rHh
元素:yIP
點(diǎn)擊查看更多內(nèi)容
5人點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦