第一次看完课程写了作业,希望以后能越学越好。写的比较简单,后面会慢慢完善的。如果有幸能获得网友的指正,不胜感激!
package com.imooc.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* Created by LinHongwei on 2017/9/14.
*/
public class useSort {
public void test(){
List<String> list=new ArrayList<String>();
Random random= new Random();
String str="";
String c = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
System.out.println("~~~~~~~排序前~~~~~~~");
for(int i=0;i<10;i++){
do{
int len=random.nextInt(10)+1;
str="";
for(int j=0;j<len;j++){
char ch=c.charAt(random.nextInt(c.length()));
str=str+ch;
}
}while(list.contains(str));
list.add(str);
System.out.println("添加元素:"+str);
}
Collections.sort(list);
System.out.println("~~~~~~~排序后~~~~~~~");
for(int i=0;i<10;i++){
System.out.println("元素:"+list.get(i));
}
}
public static void main(String[] args)
{
useSort a=new useSort();
a.test();
}
}