您的位置 首页 java

java:随机字符串加list排序

  1. public class ListSortTest {
  2. /*
  3. * 给集合添加10条随机 字符串
  4. * 每条字符串长度在10之内
  5. * 每条字符串不可重复,
  6. * 每条字符串的字符可重复
  7. */
  8. public void sort1() {
  9. List <String>string1=new ArrayList<String>();
  10. Random random=new Ran do m();
  11. String str=”0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”;
  12. int g=str.length()-1;
  13. //创建10条字符串
  14. for(int i=0;i<10;i++) {
  15. //创建元素
  16. String s=new String();
  17. do {
  18. //决定长度
  19. for (int n=0;n<random.nextInt(9)+1;n++) {
  20. //随机生成字符串str长度以内的整数
  21. int l=random.nextInt(g);
  22. //将随机生成的整形所选定的字符添加进s
  23. s=s+(str.charAt(l));
  24. }
  25. }
  26. //判断是否存在相同元素
  27. while(string1.contains(s));
  28. string1.add(s);
  29. System.out.println(“成功添加字符串”+”‘”+s+”‘”);
  30. }
  31. System.out.println(“——–排序前———–“);
  32. //打印
  33. for (int i=0;i<10;i++) {
  34. System.out.println(“元素:”+string1.get(i));
  35. }
  36. //排序
  37. Collections.sort(string1);
  38. System.out.println(“——–排序后———-“);
  39. //打印
  40. for (int i=0;i<10;i++) {
  41. System.out.println(“元素:”+string1.get(i));
  42. }
  43. }

文章来源:智云一二三科技

文章标题:java:随机字符串加list排序

文章地址:https://www.zhihuclub.com/187517.shtml

关于作者: 智云科技

热门文章

发表回复

您的电子邮箱地址不会被公开。

网站地图