import java.awt.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Sort {
public void testSort1() {
ArrayList<Integer> list1 = new ArrayList<Integer>();
Random random = new Random();
int x = 0;
for (int i = 0; i < 10; i++) {
do {
x = random.nextInt(100);
} while (list1.contains(x));
list1.add(x);
}
System.out.println("排序前");
for (Integer integer : list1) {
System.out.println(integer);
}
System.out.println("排序后");
Collections.sort(list1);
for (Integer integer : list1) {
System.out.println(integer);
}
}
public void test02() {
ArrayList<String> list2=new ArrayList<String>();
list2.add("wasdasd");
list2.add("addwwd");
list2.add("dddddw");
list2.add("saa");
System.out.println("排序前");
for (String string : list2) {
System.out.println(string);
}
System.out.println("排序后");
Collections.sort(list2);
for (String string : list2) {
System.out.println(string);
}
}
public void test03() {
//存储长度为10的字符串集
ArrayList<String> list3=new ArrayList<String>();
//从中随机抽取字符
String string="0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
//随机数
Random random=new Random();
//添加20个字符串
for(int i=0;i<20;i++){
//用来存储字符串
StringBuilder stringBuilder=new StringBuilder();
//每个字符串长度为10
for(int j=0;j<10;j++){
//1.里面的语句string.charAt() 返回指定索引处的char值
//2.random.nextInt(string.length()-1) 括号内是指定随机数的取值范围 字符串的长度-1 就是这个数组的最大索引
//也就是说 2.返回一个值 让 1. 可以获得一个char值
stringBuilder.append(string.charAt(random.nextInt(string.length()-1)));
}
//添加进集合
list3.add(stringBuilder.toString());
}
System.out.println("排序前");
for (String string2 : list3) {
System.out.println(string2);
}
System.out.println("排序后");
Collections.sort(list3);
for (String string2 : list3) {
System.out.println(string2);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Sort sort=new Sort();
sort.test03();
}
}
打开App,阅读手记