慕侠0023781
2018-07-27 17:01
public void testSort3() { List<String> stringList2 = new ArrayList<String>(); Random random = new Random(); char c ; for (int i = 0; i < 10; i++) { //往其中添加十条随机字符串 String S = ""; int length = random.nextInt(10)+1;//字符串长度为十以内的随机整数 for(int j = 0; j < length;j++) {//往每条字符串中添加内容 int n = random.nextInt(62); if(n<10) { c = (char)(n+48); } else if(n<36) { c = (char)(n+55); } else { c = (char)(n+61); } S = S + c; } System.out.println("将要添加字符串:"+S); stringList2.add(S); } System.out.println("--------排序前----------"); for (String string : stringList2) { System.out.println("元素:"+string); } Collections.sort(stringList2); System.out.println("--------排序后----------"); for (String string : stringList2) { System.out.println("元素:"+string); } }
用ASCII码输入字符不错,不过直接使用String来获取字符串在安全性以及效率上不好,最大的问题应该是在集合中添加元素时未进行重复验证
Java入门第三季
409784 学习 · 4340 问题
相似问题