這樣有問(wèn)題嗎
package com.imooc.course;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class TS {
?List <String>list =new ArrayList<String>();
?Random random=new Random();?
public? String getRandomString(int length) {
//定義一個(gè)字符串(A-Z,a-z,0-9)即62位;
? ? String str="zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
? ? //由Random生成隨機(jī)數(shù)
? ? ? ? Random random=new Random();??
? ? ? ? StringBuffer sb=new StringBuffer();
? ? ? ? //長(zhǎng)度為幾就循環(huán)幾次
? ? ? ? for(int i=0; i<length; ++i){
? ? ? ? ? //產(chǎn)生0-61的數(shù)字
? ? ? ? ? int number=random.nextInt(62);
? ? ? ? ? //將產(chǎn)生的數(shù)字通過(guò)length次承載到sb中
? ? ? ? ? sb.append(str.charAt(number));
? ? ? ? }
? ? ? ? //將承載的字符轉(zhuǎn)換成字符串
? ? ? ? return sb.toString();
}
public? ? ?void addlist() {
String i ;
int length;
for(int j=0;j<10;j++) {
do {
length=random.nextInt(10);
} while (length==0);
do{i = getRandomString(length);
}
while (list.contains(i));
{list.add(i);}
? ? ? ?}
System.out.println("list的元素依次是:");
? ? ? ? ? ? for (String string :list ) {
System.out.print(string+" ");
? ? }
? ? ? ? ? ? System.out.println(" ");
? ? ? ? ? ? Collections.sort(list);
? ? ? ? ? ? System.out.println("排序后list的元素依次是:");
? ? ? ? ? ? for (String string :list ) {
? ? System.out.print(string+" ");
? }
}
public static void main(String[] args) {
// TODO 自動(dòng)生成的方法存根
new TS().addlist();
}
}
2018-08-10
運(yùn)行了一下你的代碼,感覺(jué)沒(méi)啥問(wèn)題
2018-08-08
這個(gè)感覺(jué)不是隨機(jī)