package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class StringSort {
public static List<String> stringList=new ArrayList<String>();
//生成10个随机字符串,并添加至stringList
public void createStringList(){
String stringBasic="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int i=0;i<10;i++){
StringBuilder a=new StringBuilder();
for(int j=new Random().nextInt(10);j>=0;j--){
int choice=new Random().nextInt(62);
char b=stringBasic.charAt(choice);
a.append(b);
}
stringList.add(a.toString());
}
}
//遍历输出
public void stringForeach(){
for (String string : stringList) {
System.out.println("元素:"+string);
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
StringSort ss=new StringSort();
ss.createStringList();
System.out.println("已经随机生成的10个字符串,如下:");
ss.stringForeach();
Collections.sort(stringList);
System.out.println("排序后:");
ss.stringForeach();
}
}
打开App,阅读手记