我打了兩個(gè)代碼 一個(gè)是之前好久打的? 一個(gè)是最近打的? 最近打的這個(gè)代碼運(yùn)行不了 我不知道為什么完成功能:?? 生成N個(gè)隨機(jī)字符串? 生成后排序可以運(yùn)行的版本:????????????此版本中 字符串長(zhǎng)度是隨機(jī)的 個(gè)數(shù)是已定的?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);?? ???????????? }?? ??????? ??? ??????? ??? ???? }不可運(yùn)行版本 : ? 此版本中 字符串的長(zhǎng)度是用戶定義的 字符串的個(gè)數(shù)也是用戶定義的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("請(qǐng)定義字符串的個(gè)數(shù)");?? ??? ?int a = console.nextInt();?? ??? ?System.out.println("請(qǐng)輸入每個(gè)字符串的長(zhǎng)度 ");?? ??? ?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();?? ?}}不可運(yùn)行版本?? 加粗加下劃線部分有錯(cuò)誤? 導(dǎo)致棧內(nèi)存都崩潰了 = =
JAVA關(guān)于StringBuffer中問(wèn)題
qq_什么亡魂溺海沒(méi)有跳海實(shí)在_03283094
2017-03-03 15:25:33