遍历 Java 中的字符数组 - 改进算法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15270309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:03:18  来源:igfitidea点击:

Iterating through a character array in Java - improving algorithm

javaarraysalgorithmrecursionchar

提问by silverzx

I'm trying to iterate through my array to produce all possible combinations of the given char array.

我正在尝试遍历我的数组以生成给定 char 数组的所有可能组合。

If the length I specify is 4 then I want it to iterate through all combinations of the chars in the array up to a length of 4.

如果我指定的长度是 4,那么我希望它遍历数组中字符的所有组合,直到长度为 4。

It would look something like this:

它看起来像这样:

char[] charArray = "abcdefghijklmnopqrstuvwxyz".toCharArray();

Output of method I want:

我想要的方法的输出:

a, b, c, ..., x, y, z, aa, ab, ac, ..., ax, ay, az, ba, bb, bc, ..., bx, by, bz, ca, cb, cc, ... zzzx, zzzy, zzzz

a, b, c, ..., x, y, z, aa, ab, ac, ..., ax, ay, az, ba, bb, bc, ..., bx, by, bz, ca, cb, cc, ... zzzx, zzzy, zzzz

Here's some code:

这是一些代码:

cs = charArray;
cg = new char[4]; // 4 up to 4 characters to guess

int indexOfCharset = 0; // should I be using all these?
int indexOfCurrentGuess = 0;
int positionInString = 0;

public void incrementNew() {
    // 1 DIGIT guesses
    if (cg.length == 0) {
        if (indexOfCharset == cs.length) {
            cg = new char[cg.length + 1];
        } else {
            cg[positionInString] = nextChar();
        }
    }
    // 2 DIGIT guesses
    else if (cg.length == 1) {
        if (cg[0] == cs.length && cg[1] == cs.length) {
            cg = new char[cg.length + 1];
        } else {
            ... Something goes here <-
            cg[positionInString] = nextChar();
        }
    }
    System.out.println("cg[0]=" + cg[0]);
}

public char nextChar() {
    char nextChar;
    if (indexOfCharset < cs.length) {
        nextChar = cs[indexOfCharset];
    } else {
        indexOfCharset = 0;
        nextChar = cs[indexOfCharset];
    }
    indexOfCharset++;
    //System.out.println("nextChar = " + nextChar);
    return nextChar;

}

The only way I can think of doing it is using lots of IF statements - is there an algorithm or way to do it neater? If not then any suggestions on how to deal with two or more characters?

我能想到的唯一方法是使用大量 IF 语句 - 有没有一种算法或方法可以更简洁地做到这一点?如果没有,那么有关如何处理两个或更多字符的任何建议?

EDIT:

编辑:

I want it to work for any unsorted char arrays not just a-z.

我希望它适用于任何未排序的字符数组,而不仅仅是 az。

All the implementations I've found only work for sorted arrays..

我发现的所有实现只适用于排序数组..

采纳答案by mklnwt

You could try this:

你可以试试这个:

static char[] letters = "abcdefghijklmnopqrstuvwxyz".toCharArray();

static void getChars(char[] lastChars, int pos, int length) {
    for (char c : letters) {
        char[] newChars = lastChars.clone();
        newChars[pos] = c; // if you have "aa" for example and the current length is 4. If c = "a", newChars is now "aaa"
        if (pos + 1 < length) { // as your lenths is 4 and you still have only 3 letters, getChars adds the missing ones
            getChars(newChars, pos + 1, length);
        } else {
            System.out.println(newChars);
        }
    }
}

public static void main(String[] args) {
    int maxLength = 4;

    for (int length = 1; length <= maxLength; length++) {
        for (char c : letters) {
            if (length > 1) {
                char[] chars = new char[length];
                chars[0] = c;
                getChars(chars, 1, length);
            } else {
                System.out.println(c);
            }
        }
    }

}