ghcg
2018-04-15 09:48:59浏览 1969
package com.yjc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class CollectionsTest {
public void sort1(){
List<Integer> integerList=new ArrayList<Integer>();
Random random=new Random();
int k;
for(int i=0;i<10;i++){
do{
k=random.nextInt(100);
}while(integerList.contains(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 sort2(){
List<String> stringList=new ArrayList<String>();
stringList.add("microsoft");
stringList.add("google");
stringList.add("lenovo");
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 sort3(){
List<String> stringList=new ArrayList<String>();
String s="1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for(int i=0;i<10;){
StringBuffer sb=new StringBuffer();
Random l=new Random();
int k=l.nextInt(10);
for(int j=0;j<k;j++){
sb.append(s.charAt(l.nextInt(s.length()-1)));
}
if(stringList.contains(sb))
continue;
String newS=sb.toString();
System.out.println("元素:"+newS);
stringList.add(newS);
i++;
}
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) {
// TODO 自动生成的方法存根
CollectionsTest ct=new CollectionsTest();
// ct.sort1();
// ct.sort2();
ct.sort3();
}
}