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

简单粗暴,不要ASCII转换

public void testStringSort2(){
		//创建一个String泛型的List对象
		List<String> stringList = new ArrayList<String>();
		//创建一个字符表
		char[] character = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
				'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
		//创建一个随机数生成器
		Random random = new Random();
		//创建一个String对象接收生成字符串
		String str = "";
		//生成十个长度在10以内的随机字符串
		for(int i = 0; i < 10; i++){
			for(int j = 0; j < random.nextInt(10)+1; j++){
				str += character[random.nextInt(62)];
			}
			stringList.add(str);
			System.out.println("成功添加字符串:" + str);
			str = "";
		}
		
		//把排序前后List进行比较
		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);
		}
	}


提问者:fevernova 2016-03-07 16:14

个回答

  • fevernova
    2016-03-08 12:10:32

    Sorry, I don't understand

  • 格心派
    2016-03-08 09:33:37

    what do you want to say??