qq_慕神2245698
2020-03-04 14:54
package com.imooc6; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; public class CollectionsTesta { /** * 1.创建完List<String>之后,往其中添加十条随机字符串 * 2,每条字符串的长度为10以内的随机数 * 3.每条字符串的每个字符都为随机生产的字符,字符可以重复 * 4.每条随机字符串不可重复 */ public void testString(){ List<String> listString=new ArrayList<String>(); Random random=new Random(); Random random1=new Random(); String strs="0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"; StringBuilder str; int b; int a; for(int j=0;j<10;j++){ str=new StringBuilder(); b=random.nextInt(10)+1; for(int i=0;i<b;i++){ a=random1.nextInt(62); str.append(strs.charAt(a)); } listString.add(str.toString()); } System.out.println("-------排序前-------"); for (String string : listString) { System.out.println("元素:"+string); } Collections.sort(listString); System.out.println("-------排序后-------"); for (String string : listString) { System.out.println("元素:"+string); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub CollectionsTesta ct=new CollectionsTesta(); ct.testString(); } }
package collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.UUID;
public class collectionTest {
public void testSort3() {
List<String> stList = new ArrayList<String>();
Random random = new Random();
String randomStr;
for(int i=0;i<10;i++) {
int R = random.nextInt(10);
do {
randomStr = UUID.randomUUID().toString().substring(0, R);
}while(stList.contains(randomStr));
stList.add(randomStr);
System.out.println("将要添加字符串:"+randomStr);
}
System.out.println("-----排序前-----");
for(String s:stList) {
System.out.println("元素:"+s);
}
Collections.sort(stList);
System.out.println("-----排序后-----");
for(String s:stList) {
System.out.println("元素:"+s);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
collectionTest ct = new collectionTest();
// ct.testSort1();
// ct.testSort2();
ct.testSort3();
}
Java入门第三季
409792 学习 · 4340 问题
相似问题