package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class TestCollectionsSort {
//创建ListSting方法对字符串进行排序,字符串也有相应的要求
public void testSort(){
List<String> stringList=new ArrayList<String>();
String k;
//添加十条随机字符串
for(int i=0;i<10;i++){
do{
k=getRandomString(getInt());
}while(stringList.contains(k));
stringList.add(k);
}
System.out.println("--------随机添加的十条字符串排序前---------");
for(String string:stringList)
System.out.println("元素:"+string);
System.out.println("---------随机添加的十条字符串排序后--------");
Collections.sort(stringList);
for(String string:stringList)
System.out.println("元素:"+string);
}
//得到一个10以内的整数作为字符串创建的长度
public int getInt(){
int k;
Random random=new Random();
do{
k=random.nextInt(10);
}while(k==0);
return k;
}
//返回一个随机整数
public int getRandom(int count){
return (int)Math.round(Math.random()*(count));
}
//得到一个随机生成的字符串
public String getRandomString(int length){
String s="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
StringBuffer sb=new StringBuffer();
int len=s.length();
for(int i=0;i<length;i++){
sb.append(s.charAt(getRandom(len-1)));
}
return sb.toString();
}
//测试
public static void main(String[] args) {
// TODO Auto-generated method stub
TestCollectionsSort tcs=new TestCollectionsSort();
tcs.testSort();
}
}
點擊查看更多內(nèi)容
為 TA 點贊
評論
評論
共同學(xué)習(xí),寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦