天域知语城
2018-07-03 10:51
package randomStringSort; import java.util.Random; public class RandString { public String str; public int lenth; public RandString(){ Random rand = new Random(); this.lenth = rand.nextInt(10) + 1; StringBuilder sbd = new StringBuilder (); for (int i = 0; i<this.lenth ;i++){ char a; for (a='*';a<'0'||(a>'9'&&a<'A')||(a>'Z'&&a<'a')||a>'z';){ a = (char)(rand.nextInt(75)+48); } sbd.append(a); } this.str = sbd.toString(); 、 } }
《RandString.java》
package randomStringSort; import java.util.ArrayList; import java.util.Collections; public class Sorting { public static void main (String [] args){ ArrayList <String> strlst = new ArrayList <String>(10); OUTSIDE: for (int strCt = 0;strCt<10;strCt++){ RandString a = new RandString(); for (int strCk = 0; strCk < strCt ; strCk++ ){ if ( strlst.get(strCk).equals(a.str)){ strCt--; continue OUTSIDE; } } strlst.add(a.str); } for (int strCt = 0;strCt<10;strCt++){ System.out.println("排序前第"+(strCt+1)+"个元素是:"+strlst.get(strCt)); } Collections.sort(strlst); System.out.println("=========================="); for (int strCt = 0;strCt<10;strCt++){ System.out.println("排序后第"+(strCt+1)+"个元素是:"+strlst.get(strCt)); } } }
《Sorting.java》
Java入门第三季
409792 学习 · 4340 问题
相似问题