gvhjzdxv
2015-08-25 15:47
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; public class CollectionsTest { /** * 随机生成10个字符串。并对它进行排序。 * * @param args */ public void testSort3() { List<String> stringList = new ArrayList<String>(); String charList = "1234567890qwertyuiopasdfghjklzxcvbnm" + "QWERTYUIOPASDFGHJKLZXCVBNM"; int listLong = charList.length(); Random random = new Random(); int j = 1; while (j <= 10) { StringBuffer tempStrBuf = new StringBuffer(); for (int i = 0; i < random.nextInt(9) + 1; i++) { int k = random.nextInt(listLong); tempStrBuf.append(charList.charAt(k)); } String tempStr = tempStrBuf.toString(); stringList.add(tempStr); System.out.println("成功添加字符串:'" + tempStr + "'"); j++; } System.out.println("-----------------排序前--------------"); for (String string : stringList) { System.out.println("元素:" + string); } Collections.sort(stringList); System.out.println("-----------------排序后--------------"); for (String string : stringList) { System.out.println("元素:" + string); } } public static void main(String[] args) { CollectionsTest ct = new CollectionsTest(); ct.testSort3(); } }
感谢你
Java入门第三季
409792 学习 · 4340 问题
相似问题