输入代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionTest2 {
public List<String> stringList;
public CollectionTest2(){
this.stringList=new ArrayList<String>();
}
/**
- 生成长度小于10的字符串,共8个
-
@param args
*/
public void testRandomString(){
Random rd=new Random();for(int j=0;j<8;j++){ String k=""; int i=1+rd.nextInt(10);//生成控制字符串长度的随机数i do{ for(int x=0;x<i;x++){ int y=rd.nextInt(3); switch(y){ case 0: k+=(char)('0'+Math.random()*(10)); break; case 1: k+=(char)('a'+Math.random()*('z'-'a'+1)); break; case 2: k+=(char)('A'+Math.random()*('Z'-'A'+1)); break; } } }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); }
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("--------------------------------------");
CollectionTest2 ct=new CollectionTest2();
ct.testRandomString();
}
}