package collectiontest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsTest {
public static void main(String[] args) {
CollectionsTest clt = new CollectionsTest();
//clt.testSort1();
clt.testSort2();
}
public void testSort1() {
List<Integer> lt = new ArrayList<Integer>();
Random rd = new Random();
Integer k;
for (int i = 0; i < 10; i++) {
do {
k = rd.nextInt(100);
} while (lt.contains(k));// 为了保证不重复
lt.add(k);
System.out.println("成功添加整数" + k);
}
System.out.println("--------排序前-----------");
for (Integer i : lt) {
System.out.println("元素" + i);
}
Collections.sort(lt);
System.out.println("--------排序后-----------");
for (Integer i : lt) {
System.out.println("元素" + i);
}
}
public void testSort2() {
List<String> ltString = new ArrayList<String>();
Random rd = new Random();
StringBuffer st;
String randomString="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < 10; i++) {
do {
int length=rd.nextInt(10);
st = new StringBuffer(length);
for (int j = 0; j < length; j++) {
char temp=randomString.charAt(rd.nextInt(randomString.length()));
st.append(temp);
}
} while (ltString.contains(st));
ltString.add(st.toString());
}
for(int i=0;i<ltString.size();i++){
System.out.println("数组"+ltString.get(i));
}
System.out.println("--------排序前-----------");
for (String s : ltString) {
System.out.println("元素" + s);
}
Collections.sort(ltString);
System.out.println("--------排序后-----------");
for (String s : ltString) {
System.out.println("元素" + s);
}
}
}
点击查看更多内容
3人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦