package com.imooc.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
每条随机字符串不可重复
*/
public class CollectionTest {
public String getRandomString(){
String base = "0123456789abcdefghijklmnopqrstuvwxyz";
Random random = new Random();
int k=random.nextInt(10) ;
StringBuffer sb = new StringBuffer();
for(int i=0;i<k;i++){
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}
public void testsort3(){
List<String> stringList2 = new ArrayList<>();
CollectionTest ct = new CollectionTest();
String randomString;
//往列表里添加10个不重复的随机字符串,调用getRandomString()方法获得随机字符串
for(int i=0;i<10;i++){
do{
randomString = ct.getRandomString();
}while(stringList2.contains(randomString));
stringList2.add(randomString);
System.out.println("成功添加随机字符串:" + randomString);
}
System.out.println("----------排序前---------------");
for(String string:stringList2){
System.out.println("元素:" + string);
}
System.out.println("----------排序后---------------");
Collections.sort(stringList2);
for(String string:stringList2){
System.out.println("元素:" + string);
}
}
public static void main(String[] args) {
CollectionTest ct = new CollectionTest();
ct.testsort3();
}
}
2025 - 快车库 - 我的知识库 重庆启连科技有限公司 渝ICP备16002641号-10
企客连连 表单助手 企服开发 榜单123