Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 嵌套For循环的可变数目_Java_Loops - Fatal编程技术网

Java 嵌套For循环的可变数目

Java 嵌套For循环的可变数目,java,loops,Java,Loops,我正在用java做一个单词解读器。现在我有一个程序,可以打印从一个有3个或更多字母的单词中选择的3个字母的所有重新排列(无重复)。例如,如果参数为abcd,它将打印以下内容: [[abc、abd、acb、acd、adb、adc、bac、bad、bca、bcd、bda、bdc、cab、cad、cba、cbd、cda、cdb、dab、dac、dba、dbc、dca、dcb]] 我正在用排列填充一个2D数组列表。现在2D数组中只有一个数组,其中包含3个字母的排列。我希望2D数组有一个数组,用于排列1个

我正在用java做一个单词解读器。现在我有一个程序,可以打印从一个有3个或更多字母的单词中选择的3个字母的所有重新排列(无重复)。例如,如果参数为abcd,它将打印以下内容:

[[abc、abd、acb、acd、adb、adc、bac、bad、bca、bcd、bda、bdc、cab、cad、cba、cbd、cda、cdb、dab、dac、dba、dbc、dca、dcb]]

我正在用排列填充一个2D数组列表。现在2D数组中只有一个数组,其中包含3个字母的排列。我希望2D数组有一个数组,用于排列1个字母、2个字母、3个字母等等,在单词长度处停止。问题是我需要一个可变数量的嵌套for循环来完成这项工作。对于3个字母的排列,我有3个嵌套For循环。每一个循环遍历参数中的字母

public static void printAllPermuations(String word)
{
    int len = word.length();
    ArrayList<String> lets = new ArrayList<String>();
    //this array of letters allows for easier access
    //so I don't have to keep substringing
    for (int i = 0; i < len; i++)
    {
        lets.add(word.substring(i, i + 1));
    }

    ArrayList<ArrayList<String>> newWords = new ArrayList<ArrayList<String>>();
    newWords.add(new ArrayList<String>());
    for (int i = 0; i < len; i++)
    {
        for (int j = 0; j < len; j++)
        {
            for (int k = 0; k < len; k++)
            {
                if (i != j && i != k && j != k)
                //prevents repeats by making sure all indices are different
                {
                    newWords.get(0).add(lets.get(i) + lets.get(j) + lets.get(k));
                }
            }
        }
    }
    System.out.println(newWords);
}
publicstaticvoidprintallperumations(字符串字)
{
int len=word.length();
ArrayList lets=新的ArrayList();
//这种字母数组便于访问
//所以我不必一直打电话
对于(int i=0;i

我看过其他帖子,听说递归可以解决这个问题。不过,我不知道该如何实现这一点。我也看到了一些我不理解的复杂解决方案。我要求的是尽可能最简单的解决方案,无论它是否涉及递归。

使用递归方法,您可以将一个循环放入函数中,并将循环参数传递给该函数。然后从函数的循环中调用它的嵌套,以创建另一个循环

void loopFunction(ArrayList<ArrayList<String>> newWords, int level) {
    if (level == 0) { // terminating condition
        if (/* compare the indices by for example passing down a list with them in  */)
        {
            newWords.get(...).add(...);
        }
    } else {// inductive condition
        for (int i = 0; i < len; i++) {
            loopFunction(newWords, level-1);
        }
    }
}
编辑

因为你一直有问题,这里有一个工作版本。它保存了一个索引列表以进行比较,并在运行时构建字符串。将重新排列的单词添加到每个级别的2D数组中,以获得所有长度的单词。使用递归,最容易从功能上进行思考,不改变变量,保持变量不变(不变)。虽然为了方便起见,我更新了
索引
,而不是创建一个新的副本,但这段代码主要是这样做的

void loopFunction(ArrayList<String> lets, ArrayList<ArrayList<String>> newWords, int level, ArrayList<Integer> indices, String word) {
    if (level == 0) { // terminating condition
        return;
    } else { // inductive condition
        for (int i = 0; i < lets.size(); i++) {
            if (!indices.contains(i)) { // Make sure no index is equal
                int nextLevel = level-1;
                String nextWord = word+lets.get(i);

                newWords.get(level-1).add(nextWord);

                indices.add(i);
                loopFunction(lets, newWords, nextLevel, indices, nextWord);
                indices.remove((Integer) i);
            }
        }
    }
}
void loopFunction(ArrayList let、ArrayList新词、int-level、ArrayList索引、字符串词){
如果(级别==0){//终止条件
返回;
}else{//归纳条件
对于(int i=0;i
我不会给你实际的代码,但这应该会让你知道递归是什么样子的(我相信还有其他方法可以递归实现:p)

void findall组合(字符串tmpResult,
弦,弦,
内努夫查尔,
列出所有组合){
if(tmpResult.size()==noOfChar){
所有组合。添加(tmpResult);
}否则{
对于(0中的i到rawString.size()){
FindAllComposition(TMPRESLT+rawString[i],
rawString.removeCharAt(i),
努夫查尔,
所有组合);
}
}
}
列表foo(字符串输入,int级别){
列出所有结果=。。。;
FindAllComposition(“”,输入,级别,所有结果);
返回所有结果;
}
主要的递归部分是
findAllCombination
。这个想法是严格的。查找所有置换的方法是,对于我们拥有的原始字符串输入,我们逐个取出字符,并附加到先前找到的tmpResult,然后使用该tmpResult进行进一步处理。一旦我们发现tmpResult足够长,我们就把tmpResult放到结果
allCombinations
列表中


(foo()方法在这里只是为了让调用更简单:实际工作的p代码只有6行,不包括只有括号的行,忽略我为了更好的可读性故意打断的行)

根据@DrYap所说的,我写了这篇文章(与他的有点不同):

以下是启动它的代码:

for(int i = 1; i <= len; i++)
{
    newWords.add(new ArrayList<String>());
    loop(i);
}

for(int i=1;i if(i!=j&&i!=k&&j!=k)这似乎是错误的。loopFunction()中没有j和k局部变量。Woops对复制/粘贴有点高兴。这对尾部递归优化(对于Java 8)也有好处吗?还是应该再降低一级?您在add()中编写了代码
newWords.get(…).add(…);
)部分,我需要一个字母集合。我不知道如何收集所有的字母。有人能帮我吗?你只需将它作为参数传递。也许你可以在运行时构建字符串,这样内部循环的工作量就会减少。这就是我遇到很多麻烦的地方。我已经尝试了好几个小时了。
void findAllCombination(String tmpResult, 
                        String rawString, 
                        int noOfChar, 
                        List<String> allCombinations) {
  if (tmpResult.size() == noOfChar) {
    allCombinations.add(tmpResult );
  } else {
    for (i in 0 to rawString.size()) {
      findAllCombination(tmpResult + rawString[i], 
                         rawString.removeCharAt(i),
                         noOfChar, 
                         allCombinations);
    }
  }
}

List<String> foo(String input, int level) {
    List<String> allResults = ...;
    findAllCombination("", input, level, allResults);
    return allResults;
}
for(int i = 1; i <= len; i++)
{
    newWords.add(new ArrayList<String>());
    loop(i);
}
public static void loop(int level)
{
    if (level == 0)
    {
        int pos = indices.size() - 1;
        int pos2 = newWords.get(pos).size();
        newWords.get(pos).add("");
        for (Integer letIndex : indices)
        {
            String previous = newWords.get(pos).get(pos2);
            newWords.get(pos).set(pos2, previous + lets.get(letIndex));
        }
    }
    else
    {
        for (int i = 0; i < len; i++)
        {
            if (!indices.contains(i))
            {
                int indicesIndex = indices.size();

                indices.add((Integer) i);
                loop(level - 1);
                indices.remove(indicesIndex);
            }
        }
    }
}