长城雇佣军
2017-07-27 22:49:07浏览 2383
package list.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class StringSort
{
//创建list容器存放String字符串
public List<String> list= new ArrayList<String>();
//构造函数,用于初始化10条随机字符串
public StringSort()
{
String str = new String();
for(int i=0;i<10;i++)
{
do
{
str = CreatRandomString();
}while( list.contains(str) );
list.add(str);
}
}
//生成随机字符串
public String CreatRandomString()
{
Random random = new Random();
int size = random.nextInt(10)+1;
String str = "zxcvbnmlkjhgfdsaqwertyuiop1234567890";
StringBuilder sber = new StringBuilder();
while(size-- > 0)
{
sber.append(str.charAt(random.nextInt(str.length())));
}
return sber.toString();
}
//对10条字符串排序并且打印输出到屏幕
public void sortprint()
{
System.out.println("-------------排序前------------");
int i=1;
for(String str : list)
{
System.out.println(i+". "+str);
i++;
}
i=1;
Collections.sort(list);
System.out.println("-------------排序后------------");
for(String str : list)
{
System.out.println(i+". "+str);
i++;
}
}
public static void main(String[] args)
{
System.out.println("字符串排序程序开始!");
StringSort sst = new StringSort();
sst.sortprint();
System.out.println("字符串排序程序结束!");
}
}