课后习题,希望大家能指出不足的地方!
package com.imooc.collection; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; public class Exercise { public void test(){ List<String>stringList=new ArrayList<String>(); String s="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; char b; Random r=new Random(); for(int i=0;i<10;i++){ String a=""; do{int j=r.nextInt(10); for(int z=0;z<j;z++){ b=s.charAt(r.nextInt(61)); a=a+b; }}while(stringList.contains(a)); stringList.add(a); System.out.println("添加了元素:"+a); } 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) { // TODO Auto-generated method stub Exercise e=new Exercise(); e.test(); } }
希望大家能指出不足的地方!