簡體   English   中英

在Java中,如何創建n個具有某些屬性的字符串

[英]in Java, how to create n number of Strings with certain properties

我想做的是產生n個字符串,每個字符串應隨機包含2個字符和4個數字,例如:2w3e45 34rt56 qw5498 0126fd w5487t

依此類推,我有以下內容,它正在起作用,可以應用任何增強功能嗎? 另外,我們可以將它們作為唯一的字符串嗎?

import java.util.Random;

public class RandomNumAndChar {

public static void main(String[] args) {

    for (int j = 0; j < 10; j++) {
        Random r = new Random();
        Integer[] rn = new Integer[4];
        String numbers = new String();
        for (int i = 0; i < rn.length; i++) {
            rn[i] = r.nextInt(10);
            numbers = numbers + rn[i].toString();
        }
        char c = (char) (r.nextInt(26) + 'a');
        char c2 = (char) (r.nextInt(26) + 'a');
        String chars = "" + c + c2;
        String fin = numbers.concat(chars);
        System.out.println(RandomNumAndChar.shuffle(fin));


    }


}

public static String shuffle(String s) {
    char[] characters = s.toCharArray();
    for (int i = 0; i < characters.length; i++) {
        int randomIndex = (int)(Math.random() * characters.length);
        char temp = characters[i];
        characters[i] = characters[randomIndex];
        characters[randomIndex] = temp;
    }
    return new String(characters);
}

}

如評論中所述,這更適合CodeReview Stack Exchange 但是,以下是一些我將用來執行任務的准則:

  • 使用6個Character數組存儲代碼。 當心我說的是Character ,不是char 這是因為Java在處理包裝類時更加靈活。
  • 使用Collections#shuffle()來隨機排列數組。 這是Character出現的地方,因為我們需要使用Arrays#asList()List轉換數組。
  • 使用一種方法來生成整個代碼,不僅用於改組。

使用這個簡單的准則,您將得到類似:

class Test {

    static Random r = new Random();

    public static void main(String[] a) {

        for (int i = 0; i < 10; i++) {
            System.out.println(getNewCode());
        }
    }

    private static String getNewCode() {
        Character[] array = new Character[6];

        int i = 0;
        while(i < 4) array[i++] = (char) ('0' + r.nextInt(10));

        array[4] = (char) ('a' + r.nextInt(26));
        array[5] = (char) ('a' + r.nextInt(26));

        List<Character> l = Arrays.asList(array);

        Collections.shuffle(l);

        StringBuilder sb = new StringBuilder();

        for (char s : l)
            sb.append(s);

        return sb.toString();
    }
}

樣本輸出:

e1v456
8s632b
t7810w
n9801y
76q14c
p2a881
27m7k1
1081st
m19u36
t03h77

更方便的是,您可以直接使用List而不是數組:

class Test {

    static Random r = new Random();

    public static void main(String[] a) {

        for (int i = 0; i < 10; i++) {
            System.out.println(getNewCode());
        }
    }

    private static String getNewCode() {
        List<Character> l = new ArrayList<Character>();

        int i = 0;
        for(int i = 0; i<4; i++)
            l.add((char) ('0' + r.nextInt(10)));

        l.add((char) ('a' + r.nextInt(26)));
        l.add((char) ('a' + r.nextInt(26)));

        Collections.shuffle(l);

        StringBuilder sb = new StringBuilder();

        for (char s : l)
            sb.append(s);

        return sb.toString();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM