import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.List;import java.util.Random;public class CollectionsTest { public static Random ran = new Random(); public static List<Character> chars = new ArrayList<Character>(); static{ for(int i=48;i<=57;i++){ chars.add((char)i); } for(int i=65;i<=90;i++){ chars.add((char)i); } for(int i=97;i<=122;i++){ chars.add((char)i); } System.out.println("随机数字及字符串组成的数组:"); System.out.println(Arrays.toString(chars.toArray())); System.out.println("========================"); } public static char getRandomchar(){ int index = ran.nextInt(chars.size()); return chars.get(index); } public static String generateRandomStr(int length){ String str = ""; for(int i=0;i<length;i++){ str+=getRandomchar(); } return str; } public static List<String> generateRandomStrs(int num){ List<String> list = new ArrayList<String>(); int i=0; while(true){ String str = generateRandomStr(10); if(list.contains(str)){ continue; } list.add(str); i++; if(i == num){ break; } } return list; } public static void main(String[] args) { System.out.println("=============排序前================="); List<String> result = generateRandomStrs(10); for (String str : result) { System.out.println("输出随机数组:"+str); } System.out.println("=============排序后================="); Collections.sort(result); for (String str : result) { System.out.println("输出随机数组:"+str); } } }
1 回答
已采纳
Leiky灬
TA贡献10条经验 获得超0个赞
package Test10_29; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Random; public class Test1 { public static Random ran = new Random(); public static List<Character> chars = new ArrayList<Character>(); static { for (int i = 48; i <= 57; i++) { chars.add((char) i); } for (int i = 65; i <= 90; i++) { chars.add((char) i); } for (int i = 97; i <= 122; i++) { chars.add((char) i); } System.out.println("随机数字及字符串组成的数组:"); System.out.println(Arrays.toString(chars.toArray())); System.out.println("========================"); } public static char getRandomchar() { int index = ran.nextInt(chars.size()); return chars.get(index); } public static String generateRandomStr(int length) { String str = ""; for (int i = 0; i < length; i++) { str += getRandomchar(); } return str; } public static List<String> generateRandomStrs(int num) { List<String> list = new ArrayList<String>(); int i = 0; while (true) { //把生成str长度设置成随机数 int rand=(int) (Math.random()*100); String str = generateRandomStr(rand); if (list.contains(str)) { continue; } list.add(str); i++; if (i == num) { break; } } return list; } public static void main(String[] args) { System.out.println("=============排序前================="); List<String> result = generateRandomStrs(10); for (String str : result) { System.out.println("输出随机数组:" + str); } System.out.println("=============排序后================="); Collections.sort(result); for (String str : result) { System.out.println("输出随机数组:" + str); } } }
generateRandomStrs(int num)方法里面
把生成str长度设置成随机数
添加回答
举报
0/150
提交
取消