我打了兩個代碼 一個是之前好久打的? 一個是最近打的? 最近打的這個代碼運行不了 我不知道為什么完成功能:?? 生成N個隨機字符串? 生成后排序可以運行的版本:????????????此版本中 字符串長度是隨機的 個數是已定的?public void sorttext3(){?? ???????? List<String> a = new ArrayList<String>();?? ???????? //String[] b = new String[10];?? ???????? String st = "abcdefghijklmnopqrstuvwxyz"?? ???????????????? + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"?? ???????????????? + "0123456789";?? ???????? Random c = new Random();?? ???????? int i=0;?? ???? ??? ??????? ??? ???????????? for(int z=0;z<10;z++){?? ???????????? StringBuffer sb = new StringBuffer();?? ???????????????? do{?? ???????????????????? int j=c.nextInt(10)+1;?? ???????????????? for(;i<j;i++){?? ???????????????? //Random d = new Random(62);?? ???????????????? int num = c.nextInt(st.length());?? ???????????????? sb.append(st.charAt(num));?? ??????????????????? }?? ???????????????? }while(a.contains(sb.toString()));?? ???????????????? a.add(sb.toString());?? ??????????? }?? ???????? System.out.println("···········排序前");?? ???????????? for (String string : a) {?? ???????????????? System.out.println("元素"+a);?? ???????????? }?? ??????????? ??? ???????????? Collections.sort(a);?? ??????????? ??? ???????? System.out.println("``````````排序后");?? ??? ???????????? for (String string : a) {?? ???????????????? System.out.println("元素 "+a);?? ???????????? }?? ??????? ??? ??????? ??? ???? }不可運行版本 : ? 此版本中 字符串的長度是用戶定義的 字符串的個數也是用戶定義的public class RandomString {?? ?public void TestRomString(){?? ??? ?List<String> rs = new ArrayList<String>();?? ??? ?String c = "abcdefghiglmlnopqrsgnuwysz"?? ??? ??? ??? ?+ "ABCDEFGHIJKLMNOPQRSGWVWYS"?? ??? ??? ??? ?+ "Z0123456789";?? ??? ?Random r = new Random();?? ??? ?Scanner console = new Scanner(System.in);?? ??? ?System.out.println("請定義字符串的個數");?? ??? ?int a = console.nextInt();?? ??? ?System.out.println("請輸入每個字符串的長度 ");?? ??? ?int b = console.nextInt();//?? ??? ?StringBuffer sb = new StringBuffer();?? ??? ?for(int i = 0 ; i<a; i++){?? ??? ??? ?StringBuffer sb = new StringBuffer();?? ??? ??? ?for(int j=0;i<b;j++){?? ??? ??? ??? ?int num = r.nextInt(c.length());?? ??? ??? ???? sb.append(c.charAt(num));?? ??? ??? ?}?? ??? ??? ?String e = sb.toString();?? ??? ??? ?rs.add(e);?? ??? ??? ?if(rs.contains(e)){?? ??? ??? ??? ?i--;?? ??? ??? ??? ?continue;?? ??? ??? ?}?? ??? ?}?? ??? ?System.out.println("排序前");?? ??? ?for (String string : rs) {?? ??? ??? ?System.out.println(string);?? ??? ?}?? ??? ?System.out.println("排序后");?? ??? ?Collections.sort(rs);?? ??? ?for (String string : rs) {?? ??? ??? ?System.out.println(string);?? ??? ?}?? ?}?? ?public static void main(String[] args) {?? ??? ?RandomString rst = new RandomString();?? ??? ?rst.TestRomString();?? ?}}不可運行版本?? 加粗加下劃線部分有錯誤? 導致棧內存都崩潰了 = =
JAVA關于StringBuffer中問題
qq_什么亡魂溺海沒有跳海實在_03283094
2017-03-03 15:25:33