package com;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* @date: 日期:2016-12-13 时间:下午8:45:50
* @author yu
* 使用ASCII码生成随机字符
* 将字符转化成字符串String.valueOf(ch);
* 对字符串排序Collections.sort(strings);
*/
public class TestCollections {
public void testSort3(){
List<String> strings = new ArrayList<String>();
String str = new String();
Random random = new Random();
for(int i=0;i<10;i++){
do{
int size = random.nextInt(10);
char[] ch = new char [size];
for(int j=0;j<ch.length;j++){
ch[j]=(char) (int) (Math.random() * 127);
}
// str=ch.toString();//这样用有错误
str=String.valueOf(ch);
}while(strings.contains(str));
strings.add(str);
}
System.out.println("排序前:-------------------------------");
for (String string : strings) {
System.out.println(string);
}
Collections.sort(strings);
System.out.println("排序后:--------------------------------");
for (String string : strings) {
System.out.println(string);
}
}
public static void main(String[] args) {
TestCollections tc = new TestCollections();
tc.testSort3();
}
}
随机生成字符,将字符转化成字符串并排序
最新推荐文章于 2023-09-14 01:31:36 发布