qq_俊峰_0
2015-08-10 10:21
package xuanke;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsTest {
private Object stringList;
//数字随机数生成,并排序
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);// 随机生成0——100的随机数
} while (integerList.contains(k));// 保证每个K值不同
integerList.add(k);
System.out.println("成功添加整数:" + k);
}
System.out.println("---------------排序前----------------");
for (Integer integer : integerList) {
System.out.println("元素:" + integer);
}
Collections.sort(integerList);
System.out.println("---------------排序后----------------");
for (Integer integer : integerList) {
System.out.println("元素:" + integer);
}
}
//字母生成,并排序
public void testSort2() {
List<String> stringList = new ArrayList<String>();
stringList.add("xiao mi");
stringList.add("apple");
stringList.add("lenovo");
stringList.add("sunstare");
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 void testSort3() {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
List<String> stringList = new ArrayList<String>();
Random random = new Random();
StringBuffer n = new StringBuffer();
String k = null;
int o;
int m;
for (int j = 0; j < 8; j++) {
m = random.nextInt(10);
if (m == 0) {
for (int i = 0; i < m + 1; i++) {
do {
o = random.nextInt(62);
if (o != 0) {
k = str.substring(o, o + 1);
n = n.append(k);
} else {
continue;
}
} while (stringList.contains(k));// 保证每个K值不同
}
}
if (m != 0) {
for (int i = 0; i < m; i++) {
do {
o = random.nextInt(62);
if (o != 0) {
k = str.substring(o, o + 1);
n = n.append(k);
} else {
continue;
}
} while (stringList.contains(k));// 保证每个K值不同
}
}
stringList.add(n.toString());
System.out.println(n);
n = new StringBuffer(k);
}
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 cr = new CollectionsTest();
cr.testSort1();
cr.testSort2();
cr.testSort3();
}
}
这是sort的所有源码吗
Java入门第三季
409792 学习 · 4340 问题
相似问题