【问题标题】:Iterating through a character array in Java - improving algorithm遍历Java中的字符数组 - 改进算法
【发布时间】:2013-03-07 11:43:40
【问题描述】:

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

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

看起来像这样:

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

我想要的方法的输出:

一个, 乙, C, ..., X, 是的, z, 啊, ab, 交流, ..., 斧头, 是的, 阿兹, 巴, bb, 公元前, ..., bx, 经过, bz, 约, CB, 抄送, ... zzzx, zzzy, zzzz

这里有一些代码:

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;

}

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

编辑:

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

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

【问题讨论】:

  • 我认为这个答案中的代码:“Brute Force Algorithm w/Java Passing String Error”正是您正在寻找的。您必须将 main 方法中的一行更改为 BruteForceIterator bit = new BruteForceIterator('a', 'z', 4);
  • 你想要Power set,试试this
  • 如果我想要数字和小写字母怎么办?如果我更改字符集的顺序,它也不起作用。我希望它处理任何字符集。例如。字母、数字、字符。不只是 A-Z。

标签: java arrays algorithm recursion char


【解决方案1】:

你可以试试这个:

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);
            }
        }
    }

}

【讨论】:

  • 这是一个递归算法。对于每个长度,它都会构建 char 数组。在 main 方法中添加第一个字母。之后 getChars 为每个字母添加一个字符。如果没有达到当前长度的限制,它会再次调用getChars来获取丢失的。
  • 我将如何摆脱 for 循环: for (char c : letters) { 一旦找到我要查找的字符串?我试过使用像 innerloop: then break innerloop; 这样的循环。我也试过在外面做一个while循环,一个布尔值,一旦找到密码就会改变,但它最终还是循环!
  • “for (char c : letters) {”还有什么作用,我可以像在这篇文章中看到的那样添加一个条件吗:stackoverflow.com/a/7081497/1082213
  • 很多问题。 "for (char c : letters) {" 遍历任何可迭代对象(like 和数组)。所以每次它取出下一个元素。您可以使用“for (int i = 0; i
猜你喜欢
  • 2022-10-19
  • 2011-10-06
  • 2020-09-19
  • 2015-09-09
  • 1970-01-01
  • 1970-01-01
  • 2018-09-02
  • 1970-01-01
  • 2010-11-06
相关资源
最近更新 更多