/** *?創(chuàng)建產(chǎn)生隨機字符串的方法
*?@param?args
*/
public?String?GetRandomString(int?length)?{
????StringBuffer?string=new?StringBuffer();
????String?base="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
????Random?r=new?Random();
????int?k;??
????int?l=base.length();
????for(int?i=0;i<length;i++)?{
????k=r.nextInt(l);
????string.append(base.charAt(k));
????}
????return?string.toString();
}
/**
*?創(chuàng)建String泛型List,并進行排序
*?@param?args
*/
public?void?TestRandomStringSort()?{
List<String>?stringlist=new?ArrayList<String>();
Random?le=new?Random();
String?string;
int?length=0;
for(int?i=0;i<10;i++)?{
????do{
????length=le.nextInt(10); //隨機產(chǎn)生整數(shù)作為字符串長度,長度不為0
????}while(length==0);
????do?{
????string=GetRandomString(length); ????//調(diào)用隨機產(chǎn)生字符串的方法
????}while(stringlist.contains(string));????//字符串不能重復
????stringlist.add(string);
????}
????
????System.out.println("--------------排序前--------------");
????for?(String?stringl?:?stringlist)?{
????????System.out.println("有字符串:"+stringl);
????}
????Collections.sort(stringlist);
????System.out.println("--------------排序后--------------");
????for?(String?stringl?:?stringlist)?{
????????System.out.println("有字符串:"+stringl);
????????}
}
2019-01-03
都是一樣刷課,為何你如此優(yōu)秀。。想不到想不到。。
2019-01-03
?/**
? * 隨機生成10個字符串:這些字符串長度可以不同,字符可以重復,但是不能有兩個相同的字符串 然后對隨機生成的10個字符串排序,要求輸出排序前與排序后的列表序列
? */
?public void StringSort2() {
??Random r = new Random();
??int[] pool = new int[3];
??
??for (int j = 0; j < 10; j++) {
???String str = new String();
???do {
????/* 長度隨機 */
????int lengthRand = r.nextInt(10) + 1; // 產(chǎn)生1-10(包括10)之間的隨機數(shù)
????/* 字符隨機 */
????for (int i = 0; i < lengthRand; i++) {
?????int a = (int)(r.nextDouble() * 26) + 97; // 小寫字母ASCII碼值序列
?????int b = (int)(r.nextDouble() * 26) + 65; // 大寫字母ASCII碼值序列
?????int c = r.nextInt(10); // 0-9的數(shù)字
?????pool[0] = a;
?????pool[1] = b;
?????pool[2] = c;
?????int temp = pool[r.nextInt(3)];
?????if (temp > 9) {
??????char temps = (char) temp;
??????str = str+temps;
?????} else {
??????str = str+temp;
?????}
????}
???} while (stringSort.contains(str));
???stringSort.add(str);
???System.out.println("添加了字符串"+str);
??}
??System.out.println("排序前:");
??for (String str2:stringSort) {
???System.out.println(str2);
??}
??Collections.sort(stringSort);
??System.out.println("排序后:");
??for (String str2:stringSort) {
???System.out.println(str2);
??}
?}
?public static void main(String[] args) {
??CollectionsTest test = new CollectionsTest();
??// test.addSort1();
??// test.StringSort1();
??test.StringSort2();
?}
}
2018-12-16
public class Text {
String num= "123456789qwertyuiopasdfgh"
+ "jklasdfghjklzxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";
public List<String> ch;
public Text() {
ch=new ArrayList<String>();
}
public void add() {
int n=num.length(); //字符長度
Random a=new Random();
for(int i=0;i<10;i++) {
String at="";
String aa=new String();
do{
for(int j=0;j<(a.nextInt(10)+1);j++) {
at=num.charAt(a.nextInt(n))+"";
aa+=at;
}
}while(ch.contains(aa));
ch.add(aa);
}
Collections.sort(ch);
for(String aa:ch) {
System.out.println(aa);
}
}
2018-12-12
完美,nice
2018-11-30
親,同是新人,我看著你的代碼沒有什么問題。