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

长度10以内的10个不同长度的字符串

package src.com.cg;

import java.util.*;

public class ListSort {
    public static void main(String[] args) {
        //定义一个字符串包含 数字和字母
        String str = "zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        List<String> list = new ArrayList<>();
        ListSort sort = new ListSort();
        //添加元素
        sort.add(str, list);
        System.out.println(list.size());
        for (String s : list) {
            System.out.println("排序前的字符串是" + s);
        }
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length() - o2.length();
            }
        });
        for (String s : list) {
            System.out.println("排序后的字符串是:" + s);
        }
    }


    private void add(String str, List<String> list) {
        Random random = new Random();
        do {
            StringBuilder sb = new StringBuilder();
            // 随机生生成一个10以内的长度
            int i = random.nextInt(11);
            //用这个长度去循环遍历生成该长度的字符串
            for (int i1 = 0; i1 < i; i1++) {
                int i2 = random.nextInt(62);
                sb.append(str.charAt(i2));
            }
            //过滤掉空字符串和重复的字符串
            if (!sb.toString().isEmpty() && (!list.contains(sb.toString()))) {
                //保证每个元素的长度都是不一样的
                if (list.isEmpty()) {
                    list.add(sb.toString());
                } else {
                    boolean flag = false;
                    for (int i4 = 0, length = list.size(); i4 < length; i4++) {
                        if ((list.get(i4).length() == sb.length())) {
                            flag = true;
                        }
                    }
                    if (!flag) {
                        list.add(sb.toString());
                    }
                }
            }
        } while (list.size() != 10);

    }


}


提问者:杀手cool 2019-01-05 14:10

个回答

  • preatiry
    2019-02-19 15:12:41

    public void testSort4() {

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

    Random random = new Random();

    char[] charList = {'a','b','c','d','e','f','d','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};


    String string = "";

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

    int bit = random.nextInt(10) + 1;

    do {

    string = "";

    for(int j = 0 ; j <bit ; ++j) {

    string += charList[random.nextInt(26)];

    }

    }while(stringList.contains(string));

    stringList.add(string);

    }


    System.out.println("--------------排序前---------------");

    for (String str : stringList) {

    System.out.println("字母:" + str);

    }

    Collections.sort(stringList);

    System.out.println("--------------排序后---------------");

    for (String str : stringList) {

    System.out.println("字母:" + str);

    }

    }


  • 慕粉3796571
    2019-01-11 18:48:59

    package com.imooc.collection;


    import java.util.ArrayList;

    import java.util.Collections;

    import java.util.Iterator;

    import java.util.List;

    import java.util.Random;


    public class CollectionsTest {

    /**

    * 泛型为Integer的List排序

    * @param args

    */

    public void testSort1(){

    List<Integer> list = new ArrayList<Integer>();

    Random random = new Random();

    int k;

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

    do{

    k = random.nextInt(100);

    }while(list.contains( k));

    list.add(k);

    System.out.println("随机数:"+k);

    }

    //排序前遍历随机数

    System.out.println("--------排序前遍历随机数------");

    for (Integer integer : list) {

    System.out.println("随机数:"+integer);

    }

    //集合排序

    Collections.sort(list);

    //排序后遍历随机数

    System.out.println("--------排序后遍历随机数------");

    Iterator<Integer> iterator = list.iterator();

    while(iterator.hasNext()){

    System.out.println("随机数:"+iterator.next());

    }

    }

    /**

    * 泛型为字符串的排序

    * @param args

    */

    public void testSort2(){

    ArrayList<String> listStr = new ArrayList<String>();

    listStr.add("jenny");

    listStr.add("Mark");

    listStr.add("tom");

    System.out.println("--------排序前遍历字符串------");

    for (String string : listStr) {

    System.out.println("字符串:"+string);

    }

    Collections.sort(listStr);

    System.out.println("--------排序后遍历字符串------");

    for (String string : listStr) {

    System.out.println("字符串:"+string);

    }

    }

    /**

    * 是个字符串,长度是1-10

    * @param args

    */

    public void testSort3(){

    List<String> list = new ArrayList<String>();

    Random random = new Random();

    String use = "0123456789asdfghjklzxcvbnmqwertyuiopASDFGHJKLQWERTYUIOPXZCVBNM";

    char[] charArray = use.toCharArray();

    int b = charArray.length;

    int k;

    int index;

    String bb="";

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

    do{

    k = random.nextInt(10);

    for(int j=0;j<=k;j++){

     index = random.nextInt(charArray.length);

     char c = charArray[index];

     bb+=c;

    }

    }while(list.contains(bb));

    list.add(bb);

    System.out.println("随机字符串:"+bb);


    bb="";

    }

    System.out.println("----------排序前遍历字符串---------");

    for (String str : list) {

    System.out.println("随机字符串:"+str);

    }

    System.out.println("----------排序后遍历字符串---------");

    Collections.sort(list);

    for (String str : list) {

    System.out.println("随机字符串:"+str);

    }

    }


    public static void main(String[] args) {

    // TODO Auto-generated method stub

    CollectionsTest collectionsTest = new CollectionsTest();

    //collectionsTest.testSort1();

    //collectionsTest.testSort2();

    collectionsTest.testSort3();

    }


    }