红袖侍读
2016-06-14 04:18
package com.course; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Random; public class ListSort { List<String> stringList = new ArrayList<String>(); public final String allChar = "01234567abcdefghijklmnopqrstuvwxyzABCKEFGHIJKLMNOPQRSTUVWXYZ"; public final int NUM = 10; public final int MAX = 10; public String genOneString(){ StringBuffer sb = new StringBuffer(); int len; Random ran = new Random(); len = ran.nextInt(10); //gernate the length of a string while (len<1){ //string length shouldn't be 0 len = ran.nextInt(10); } for (int i=0;i<len;i++) sb.append(allChar.charAt(ran.nextInt(allChar.length()))); return sb.toString(); } public void genStringList(){ String s; Random ran=new Random(); for (int i=0;i<NUM;i++){ s = genOneString(); while(stringList.contains(s)){ s = genOneString(); } stringList.add(s); } } public void sortList(){ Collections.sort(stringList); } public void displayStringList(){ for (String s:stringList){ System.out.println(s); } } public static void main(String[] args) { // TODO Auto-generated method stub ListSort ls = new ListSort(); ls.genStringList(); System.out.println("********befor sorting********"); ls.displayStringList(); ls.sortList(); System.out.println("********after sorting********"); ls.displayStringList(); } }
没有注释,看得我有点头晕,还几次推算的结果跟实际运行的结果不一样。比我强多了,我就不能做出来。17-24行是不是可以改为
int len;
Random ran = new Random();
do{
len = ran.nextInt(10); //gernate the length of a string
}while (len<1);
其他的我还得在学习学习
Java入门第三季
409792 学习 · 4340 问题
相似问题