package com.imooc.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsTest {
public void strSort() {
List<String> str = new ArrayList<String>();
for(int i = 0;i < 10;i++) {
String s = getStr(count());
do {
if(!(str.contains(s))) {
str.add(s);
System.out.println("成功添加字符:" + s);
}
}while(s == "");
}
System.out.println("排序前----------------");
fore(str);
Collections.sort(str);
System.out.println("排序后----------------");
fore(str);
}
public String getStr(int length) {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for(int i = 0;i < length;i++) {
int number = random.nextInt(62);
sb.append(str.charAt(number));
}
return sb.toString();
}
public int count() {
Random random = new Random();
int count = 0;
do {
count = random.nextInt(10);
}while(count == 0);
return count;
}
public void fore(List<String> x) {
for(String s:x) {
System.out.println("元素:" + s);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CollectionsTest ct = new CollectionsTest();
ct.strSort();
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章