简体   繁体   English

生成具有指定字符的随机字符串?

[英]Generate random strings with specified characters?

Well, I got stuck on how to generate strings with all possible values with the specified characters. 好吧,我被困在如何生成带有指定字符的所有可能值的字符串。 Okay, I don't know how to explain it well, so here is an example: 好的,我不知道如何很好地解释它,所以这里有一个例子:

chars: a, b, c 字符: a, b, c

generation: 代:

abc
aab
abb
acc
baa
bbb
bbc
bac
cab
ccc
aaa
cbb
caa
ccb
cca
bab
bcb

I've tried using a character list, and then iterating every character in this list, and then iterate again every character on the list, but... let's say it doesn't worked. 我尝试过使用字符列表,然后迭代此列表中的每个字符,然后再次迭代列表中的每个字符,但是...让我们说它不起作用。

A copy from my own answer on this question : 我对这个问题的回答的副本:

This will work for a,b,c or any other continuous sequence of characters: 这适用于a,b,c或任何其他连续字符序列:

import java.util.Arrays;
import java.util.Iterator;

public class BruteForceIterator implements Iterator<String> {

    private char min, max;

    private char[] current;

    private char[] last;

    private int reachedLast = 0;

    public BruteForceIterator(char min, char max, int length) {
        this.min = min;
        this.max = max;
        current = new char[length];
        Arrays.fill(current, min);
        last = new char[length];
        Arrays.fill(last, max);
    }

    @Override
    public boolean hasNext() {
        return reachedLast < 2;
    }

    @Override
    public String next() {
        String str = new String(current);
        for(int i = current.length - 1; i >= 0; i--) {
            char next = following(current[i]);
            current[i] = next;
            if (next != min) {
                break;
            }
        }
        if (Arrays.equals(current, last) || reachedLast > 0) {
            reachedLast++;
        }
        return str;
    }

    private char following(char in) {
        if (in < max) {
            return (char) (in + 1);
        } else {
            return min;
        }
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("No with me, sir!");
    }

    public static void main(String[] args) {
        BruteForceIterator bit = new BruteForceIterator('a', 'c', 3);
        while (bit.hasNext()) {
            System.out.println(bit.next());
        }
    }
} 

I quickly made this code below. 我很快在下面做了这段代码。 You might have to edit it a bit for it to work or adapt to your needs I just wanted to share the idea of my code with you. 您可能需要对其进行一些编辑才能使其工作或适应您的需求,我只想与您分享我的代码想法。 However I would recommend jlordo's answer because its more efficient. 但是我建议jlordo的答案,因为它更有效。

Array String[] = new Array[]{"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"}
ArrayList<String> numbersrandom;
for(int a = 0; i <= 26; i++){
for(int b = 0; i <= 26; i++){
for(int c = 0; i <= 26; i++){
numbersrandom.add(String[a]+String[b]+String[c]);
}
}
}

After using this code, you can use a random generator and for loop to randomly mix these. 使用此代码后,可以使用随机生成器和for循环将它们随机混合。 Good Luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM