简体   繁体   English

Java递归生成字符串-所有1以及1和0

[英]Java Generating Strings Recursively - All 1s then 1 and 0

Hey guys I'm trying to get the concept of recursion down by making a program that generates String of an ArrayList recursively. 大家好,我试图通过制作一个递归生成ArrayList String的程序来降低递归的概念。 My basic algorithm is: 我的基本算法是:

public static ArrayList<String> generateListOfAll1sStrings(int maxBits)

terminal condition: if maxBits is 1, return the simplest case: a list containing just "1" 终端条件:如果maxBits为1,则返回最简单的情况:仅包含“ 1”的列表

otherwise: recursively call generateListOfAll1sStrings() for the next-smallest bit-length, saving the list that is returned find the longest string in that list and create a new string with "1" appended to it (making the next-longest string) return a new list that contains all the elements of the shorter list along with the new string just added. 否则:递归地调用generateListOfAll1sStrings()以获取下一个最小的位长,保存返回的列表,以找到该列表中最长的字符串,并创建一个附加了“ 1”的新字符串(使下一个最长的字符串)返回一个新列表,其中包含较短列表的所有元素以及刚刚添加的新字符串。

The code I have so far is: 到目前为止,我的代码是:

    package bincomb.model;

    import java.util.ArrayList;

    public class BinaryCombinationGenerator {

public static ArrayList<String> generateListOfAll1sStrings(int maxBits) {
    String string = null;
    ArrayList<String> listofJust1 = new ArrayList<String>();
    ArrayList<String> otherArray = new ArrayList<String>();
    int i = 1;


    if (maxBits == 1) {
        listofJust1.add("1");
        return listofJust1;
    }

    if (maxBits > 1) {
        for (String string2 : listofJust1) {
            String comp = "";
            if (!(comp.equals(string2))) {
                comp = string2;
            }
            string = comp;
        }
        listofJust1.add(i, (string + "1"));
        i++;
        listofJust1 = BinaryCombinationGenerator.generateListOfAll1sStrings((maxBits-1));

        System.out.println(listofJust1);
        return listofJust1;
    }

    return listofJust1;
}

public static void main(String[] args) {
    generateListOfAll1sStrings(10);
}

}

However, currently, I'm returning an IndexOutOfBoundsException . 但是,目前,我正在返回IndexOutOfBoundsException I think my for loop is causing the problem, but I'm not certain how to go about fixing it. 我认为我的for循环引起了问题,但是我不确定如何解决它。

You're getting an java.lang.IndexOutOfBoundsException at this line listofJust1.add(i, (string + "1")); 您在此行listofJust1.add(i, (string + "1"));收到java.lang.IndexOutOfBoundsException listofJust1.add(i, (string + "1")); .

This is because the method list.add(index, objects) tries to add the object at index "1" but your array has 0 elements. 这是因为方法list.add(index,objects)尝试将对象添加到索引“ 1”,但是您的数组有0个元素。

Either change it to listofJust1.add(i-1, (string + "1")); 要么将其更改为listofJust1.add(i-1, (string + "1")); or simply listofJust1.add((string + "1")); 或者只是listofJust1.add((string + "1"));

@Edit: here: @编辑:在这里:

listofJust1.add(i, (string + "1"));

You want to add the string for the current (N) level of recursion but below you substitute this array with: 您想为当前(N)级递归添加字符串,但在下面用以下命令替换此数组:

listofJust1 = BinaryCombinationGenerator.generateListOfAll1sStrings((maxBits-1));

Which basically says "get the result for (maxBits-1) and replace with it listofJust1" therefore you are losing what you added before. 基本上说“获取(maxBits-1)的结果并用listofJust1替换”,因此您将丢失之前添加的内容。

Instead you should first get the list for level N-1 and then add the string for the current level: 相反,您应该首先获取级别N-1的列表,然后添加当前级别的字符串:

listofJust1 = BinaryCombinationGenerator.generateListOfAll1sStrings((maxBits-1));
listofJust1.add(stringForThisLevel);

Also you need to rething how you are computing "string" at level N, doesn't seem right. 另外,您还需要重新思考如何在N级计算“字符串”,这似乎不正确。

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

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