public void testSort2(){
List<String> stringList=new ArrayList<String>();
Random r1=new Random();
Random r2=new Random();
String base="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int length=base.length();
for(int i=0;i<10;i++){
StringBuffer sb=new StringBuffer();
do{
for(int j=0;j<r1.nextInt(10);j++){
int num=r2.nextInt(length);
sb.append(base.charAt(num));
}
}while(stringList.contains(sb.toString()));
stringList.add(sb.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);
}
}
热门评论
-
CapricornusQi2017-08-23 0
查看全部评论//导入java.util.Arrays; import java.util.Arrays; public class HelloWorld { public static void main(String[] args) { // 创建对象,对象名为hello HelloWorld hello = new HelloWorld(); // 调用方法并将返回值保存在变量中 int[] nums = hello.getArray(8); // 将数组转换为字符串并输出 System.out.println(Arrays.toString(nums)); } /* * 功能:创建指定长度的int型数组,并生成100以内随机数为数组中的每个元素赋值 * 定义一个带参带返回值的方法,通过参数传入数组的长度,返回赋值后的数组 */ public int[] getArray(int length) { // 定义指定长度的整型数组 int[] nums = new int[length]; // 循环遍历数组赋值 for (int i=0;i<nums.length;i++) { // 产生一个100以内的随机数,并赋值给数组的每个成员 nums[i]=(int)(Math.random()*100); } return nums; // 返回赋值后的数组 } }