作业要求:
实现代码:
package JiHe;
import java.util.List;
import java.util.Collections;
import java.util.ArrayList;
import java.util.Random;
public class CollectionSort {
Random rd=new Random();
List<String> costs=new ArrayList<String>();
StringBuilder sb=new StringBuilder();
public void test(){
String a="0123456789ABCDEFGHIJKLMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for(int j=0;j<10;j++){
//清空StringBuilderl类的对象sb
sb.delete(0, 10);
//随机生成一个长度为10以内的数组(包括10)
int d=rd.nextInt(11);
for(int i=0;i<d;i++){
//随机获取字符串a中的某个字符的位置
int b=rd.nextInt(61);
//将获取到的字符添加到StringBuilder类对象中
sb.append(a.charAt(b));
}
//如果集合中已存在该字符串或者字符串为空,重新执行该次循环
if(costs.contains(sb.toString())||d==0){
j--;
}else{
//将StringBuilder对象转化成String类型并添加到List集合中
costs.add(sb.toString());
}
}
//用Collections.sort()方法对List排序
Collections.sort(costs);
//foreach遍历输出集合中的元素
for(String cost:costs){
System.out.println(cost);
}
}
//入口函数
public static void main(String[] args) {
CollectionSort cts=new CollectionSort();
cts.test();
}
}
运行结果:
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦