package test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsTest {
public void testSort1() {
List<Integer> integerList = new ArrayList<Integer>();
Random random = new Random();
Integer k;
for (int i = 0; i < 10; i++) {
do {
k = random.nextInt(100);
} while (integerList.contains(k));
integerList.add(k);
System.out.println("成功");
}
System.out.println("排序前");
for (Integer integer : integerList) {
System.out.print(integer + " ");
}
Collections.sort(integerList);
System.out.println("排序后");
for (Integer integer : integerList) {
System.out.print(integer + " ");
}
}
public void TestSort2() {
List<String> stringList = new ArrayList<String>();
String a = "";
int k; //字的个数
int charnum; //字符串中字符的个数
Random random = new Random();
for (int i = 0; i < 10; i++) {
do {
charnum = random.nextInt(10);
a = "";
for (int j = 0; j < charnum; j++) {
k = random.nextInt(123);
if ((k >= 48 && k <= 57)
(k >= 65 && k <= 90)
(k >= 97 && k <= 122)) {
a = a+(char) k;
}
}
} while (stringList.contains(a));
stringList.add(a);
System.out.println("添加成功");
}
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) {
CollectionsTest ct = new CollectionsTest();
ct.TestSort2();
}
}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦