第三季6.5collections.sort习题

306 查看

package com.imooc.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class CollectionsTest
{
public void testSortP()
{
List<String>str=new ArrayList<String>();
String strr[] = { "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", "0", "1", "2", "3", "4",
"5", "6", "7", "8", "9" };

for(int j=0;j<10;j++)
{
  String finalString=" ";
  int len=(int)((Math.random()*10)+1);
  for(int i=0;i<len;i++)
  {
     String finalString=" ";
  int len=(int)((Math.random()*10)+1);
  for(int i=0;i<len;i++)
  {
      int lens=(int)(Math.random()*62);
          do{finalString+=strr[lens];}
          while(str.contains(finalString));

  }   
  str.add(finalString);

  }   
  str.add(finalString);
}

  for(String string:str)
  {
      System.out.println("Pre-sort random string is: "+ string);
  }

  Collections.sort(str);

  for(String string:str)
  {
      System.out.println("Post-sort random string is: "+ string);
  }

}

public static void main(String[] args)
{
    CollectionsTest ct=new CollectionsTest();
    ct.testSortP();

}

}