简体   繁体   中英

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. 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"

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.

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 . I think my for loop is causing the problem, but I'm not certain how to go about fixing it.

You're getting an java.lang.IndexOutOfBoundsException at this line 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.

Either change it to listofJust1.add(i-1, (string + "1")); or simply 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:

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.

Instead you should first get the list for level N-1 and then add the string for the current level:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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