package studentCourse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsTest {
/**
- 将要完成:
- 通过Collections.sort()方法,对String泛型的List进行排序;
- 1.创建完List<String>之后,往其中添加十条随机字符串
- 2.每条字符串的长度为10以内的随机数
- 3.每条字符串的每个字符都为随机生成的字符,字符可以重复;
-
4.每条随机字符串不可以重复
*/public void testSort() { List<String> list = new ArrayList<String>(); String str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; Random random = new Random(); String s=""; int n; int m; for(int i=0;i<10;i++){ n=random.nextInt(10)+1; s=""; do{ for ( int j = 0; j< n; j++) { m= random.nextInt(62); char ch=str.charAt(m); s=s+ch; } }while(list.contains(s)); list.add(s); System.out.println("成功添加第"+(i+1)+"个字符串:" + s); } System.out.println("-------------排序前--------------"); for (String str0 : list) { System.out.println("元素:" + str0); } Collections.sort(list); System.out.println("----------------排序后-------------------"); for (String str1 : list) { System.out.println("元素:" + str1); } }
public static void main(String[] args) {
CollectionsTest ct = new CollectionsTest();
ct.testSort();}
}