import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; public class randomstring { String possible="abcdefghijklmno" + "pqrstuvwxyzABCDEFGHIJKLMNO" + "PQRSTUVWXYZ1234567890"; List<String> string=new ArrayList<String>(); String tempstring2; public void addstring(){ for(int i=0;i<10;i++){ System.out.println("这是第"+(i+1)+"次循环"); do{ int digits = new Random().nextInt(10) + 1; //System.out.println(digits); StringBuilder tempstring=new StringBuilder(); for (int j=0 ;j<digits;j++){ int num = new Random().nextInt(62); tempstring.append(possible.charAt(num)); } System.out.println("生成了"+tempstring); String tempstring2=tempstring.toString(); } while(string.contains(tempstring2)); string.add(tempstring2); System.out.println("添加了字符串"+tempstring2); } } public void sortstring(){ System.out.println("--------排序前的字符串为-----------"); for(String a:string){ System.out.println(a); } System.out.println("--------排序后的字符串为-----------"); Collections.sort(string); for(String a:string){ System.out.println(a); } } public void testadd(){ String a="abc"; String b="ab"; string.add(a); System.out.println(string.contains(b)); } public static void main(String[] args) { // TODO Auto-generated method stub randomstring rs=new randomstring(); rs.addstring(); rs.sortstring(); //rs.testadd(); } }
慕粉3392422
相关分类