问答详情
源自:6-5 应用 Collections.sort() 实现 List 排序

渣渣表示第一次发代码,随便写了点

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Random;



public class CollectionSortTest {


static List<String> stringList = new ArrayList<String>();

static Random random = new Random();


public static void getRandomString(int length){

String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

StringBuffer sb = new StringBuffer();

do{

for (int i=0;i<length;i++){

int number;

number = random.nextInt(62);

sb.append(str.charAt(number));

}

}while(stringList.contains(sb));

System.out.println("将要添加的字符串:" + sb.toString());

stringList.add(sb.toString());

}

public static void main(String args[]) {

for (int i=0;i<10;i++) {

int length = random.nextInt(10);

getRandomString(length);

}

System.out.println("---------执行前-----------");

for (int i=0;i<10;i++) {

System.out.println(stringList.get(i).toString());

}

System.out.println("---------执行后------------");

Collections.sort(stringList);

for (int i=0;i<10;i++) {

System.out.println(stringList.get(i).toString());

}

}

}


提问者:专属小小苏 2015-02-06 13:29

个回答

  • liwei9902
    2015-12-18 09:06:09

    不错哟