Java - MOOC example - sorting list elements

0. description

/**
 *Target: sort string list elements;
 *Steps: 1. Randomly generate 10 pairs of strings;
 *2. Call sort method to sort;
 *Requirements: the generated string contents are different; the characters can be the same; the string length is within 10;
 */

1. Procedures and results

import java.util.*;

public class CollectionsTest {
    /**
     * Target: sort string list elements;
     * Steps: 1. Randomly generate 10 pairs of strings;
     * 2.Call sort method to sort;
     * Requirements: the generated string contents are different; the characters can be the same; the string length is within 10;
     */
    public static void main(String[]args){
        List list = generateStringList();
        sortStringList(list);
    }
    //The method of sorting and printing
    public static void sortStringList(List<String> list){
        System.out.println("Pre ranking list Element is:"+ Arrays.toString(list.toArray()));
        Collections.sort(list);
        System.out.println("-----------------------");
        System.out.println("After sorting list Element is:"+ Arrays.toString(list.toArray()));
    }
    //Randomly generate a list of 10 strings
    public static List generateStringList(){
        List<String> list = new ArrayList<String>();
        Random rand = new Random();
        for(int i=0;i<10;i++){
            String newStr = "";
            do {
                int randLen = rand.nextInt(10);
                for (int j = 0; j <= randLen; j++) {
                    int randChar = 'a' + rand.nextInt(25);
                    newStr += (char) randChar;
                }
                list.add(newStr);
            }while(!list.contains(newStr));
        }
        return list;
    }
}
Pre ranking list Element is:[kcuit, tpoap, jtqj, mpadxu, uoydtjcyf, bk, ej, adk, xhjhwm, kedhgs]
-----------------------
//The sorted list elements are: [adk, bk, ej, jtqj, kcuit, kedhgs, mpadxu, tpoap, uoydtjcyf, xhjhwm]

Process finished with exit code 0

2. improvement

The random string generated by the above method only includes lowercase letters;
We want to add numbers and capital letters.

import java.util.*;

public class CollectionsTest2 {
    /**
     * Improvement: the generated string should also include uppercase letters and numbers
     */
    public static void main(String[]args){
        List list = generateStringList();
        sortStringList(list);
    }
    //The method of sorting and printing
    public static void sortStringList(List<String> list){
        System.out.println("Pre ranking list Element is:"+ Arrays.toString(list.toArray()));
        Collections.sort(list);
        System.out.println("-----------------------");
        System.out.println("After sorting list Element is:"+ Arrays.toString(list.toArray()));
    }
    //Randomly generate a list of 10 strings
    public static List generateStringList(){
        List<String> list = new ArrayList<String>();
        Random rand = new Random();
        for(int i=0;i<10;i++){
            String newStr = "";
            do {
                int randLen = rand.nextInt(10);
                for (int j = 0; j <= randLen; j++) {
                    int randCharType = rand.nextInt(3);//Random 0,1,2 represent lower case, upper case and number respectively
                    int randChar;//Store randomly generated characters
                    switch(randCharType){
                        case 0://A lowercase letter
                            randChar = 'a' + rand.nextInt(25);
                            newStr += (char) randChar;
                            break;
                        case 1://Capitalization
                            randChar = 'A' + rand.nextInt(25);
                            newStr += (char) randChar;
                            break;
                        case 2://number
                            randChar = '0' + rand.nextInt(9);
                            newStr += (char) randChar;
                            break;
                    }
                }
                list.add(newStr);
            }while(!list.contains(newStr));
        }
        return list;
    }
}
Pre ranking list Element is:[w, rOG6C8L, 3L5a0, 25Eso47j8Q, Pad, 3, e3m1, 5Lc, j7RBbwE, KcNvXJP]
-----------------------
//The sorted list elements are: [25Eso47j8Q, 3, 3L5a0, 5Lc, KcNvXJP, Pad, e3m1, j7RBbwE, rOG6C8L, w]

Process finished with exit code 0

In fact, there are other options for the realization of randomly generating upper and lower case letters and numbers;
For example, if you put 0-9a-zA-Z into an array or a string, you can also randomly generate an integer of 0-61.

Tags: Java

Posted on Fri, 08 Nov 2019 08:54:18 -0800 by 1981tarun