手记

自动生成字符串随机排序的作业

package com.imooc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class CollectionsTest {
public void randomString(){
List<String> stringList = new ArrayList<String>();
String keyString="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int len = keyString.length();
for(int i=0;i<10;i++){
Random random= new Random();
int siz=random.nextInt(9)+1;
StringBuffer rs = new StringBuffer();
for(int j=0;j<siz;j++){
rs.append(keyString.charAt((int) Math.round(Math.random() * (len - 1))));
}
System.out.println("将要添加字符串:"+rs);
stringList.add(rs.toString());
}
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 Auto-generated method stub

/*

  • */
    CollectionsTest ct = new CollectionsTest();
    ct.randomString();
    }

}

0人推荐
随时随地看视频
慕课网APP