交作業(yè)~~~
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/*
?* 1.創(chuàng)建完List<string>之后,往其中添加十條隨機(jī)字符串
?* 2.每條字符串的長度為10以內(nèi)的隨機(jī)整數(shù)
?* 3.每條字符串的每個字符都為隨機(jī)生成的字符,字符可以重復(fù)
?* 4.每條隨機(jī)字符串不可重復(fù)
?*/
public class ExceciseCollections {
List<String> stringList=new ArrayList<String>();
public void testSort() {
String a="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random=new Random();
for(int i=0;i<10;i++) {
StringBuilder sb=new StringBuilder();
do {
sb.delete(0, sb.length());
int strlen=random.nextInt(10)+1;
if(strlen==0)continue;
for(int k=0;k<=strlen;k++) {
int cursor=random.nextInt(a.length());
sb.append(a.charAt(cursor));
}
}while(stringList.contains(sb.toString()));
stringList.add(sb.toString());
}
System.out.println("---------排序前------------");
for(String str:stringList) {
System.out.println("序列"+str);
}
Collections.sort(stringList);
System.out.println("-----排序后----------");
for(String str:stringList) {
System.out.println("序列"+str);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ExceciseCollections ec=new ExceciseCollections();
ec.testSort();
}
}
2022-04-14
你的這個包的名都沒有說清楚呀,這個作業(yè)不好