Firefan
Firefan

Reputation: 57

Java - Content of List is lost in recursion

I'm currently programming on a little project (which is way to specific to explain here) and I got everything working except one part. I've got a List pZiegel by parameter which is modified in recursion. Because it didn't work, I did a little debugging and found the problem: At one point, the list contains exactly one number at the end of the method. Then, the program jumps one recursion depth back. And directly after that, it doesn't contains any numbers anymore. How did it lose the number? Lists as parameters work with pass-by-reference, so it shouldn't just reject it, right?

public void erstelleBaum (Tree pTree, List<Integer> pZiegel, List<Integer> pFugen, int tiefe) { 
    if (tiefe / n >= maxHoehe) {
        System.out.println("hi");
        mauerGefunden = true;
        alleFugen = pFugen;
    }
    if (!mauerGefunden) {

        pZiegel.toFirst();
        while (pZiegel.hasAccess() && !mauerGefunden) {
            boolean ziegelHinzufügen = false;
            möglich = true;
            aktZiegel = pZiegel.getContent();

            // ...

            if (möglich) {

                // ...

                pZiegel.remove();

                if (pZiegel.isEmpty()) { 
                    ziegelHinzufügen = true;
                    pZiegel = new List();
                    for (int i = 1; i <= n; i++) {
                        pZiegel.append(i);
                    }
                }


                // Recursion
                erstelleBaum(neuesBlatt, pZiegel, neueFugen, neueTiefe);

                // Here, it tells me that pZiegel is empty (at recursion depth 17)

                if (ziegelHinzufügen) {
                    pZiegel.toFirst();
                    while (pZiegel.hasAccess()) {
                        pZiegel.remove();
                    }
                    pZiegel.append(aktZiegel);
                }
                else {

                    pZiegel.toFirst();
                    while (pZiegel.hasAccess() && pZiegel.getContent() < aktZiegel) {
                        pZiegel.next();
                    }
                    if (pZiegel.hasAccess()) {
                        pZiegel.insert(aktZiegel);
                        pZiegel.toFirst();
                        while (pZiegel.getContent() != aktZiegel) {
                            pZiegel.next();
                        }
                    }
                    else {               
                        pZiegel.toLast();
                        pZiegel.append(aktZiegel);
                        pZiegel.toLast();
                    }
                }
            }              
            pZiegel.next();
        }
    }
    // Here, pZiegel contained one number (at recursion depth 18)
}

I hope, the code isn't too messy. I tried to keep out the parts that doesn't involve pZiegel. And sorry, that the variables are named in german. I didn't want to change them for this post because I know I would forget to change something in the code.

Feel free to ask, if something is unclear.

Upvotes: 0

Views: 190

Answers (1)

Rafael Odon
Rafael Odon

Reputation: 1360

I believe the pZiegel List reference is being lost at some point. You should check the pZiegel object ID (a number displayed when you inspect the object) to make sure it is the same List instance all over the recursions.

Notice that there's one part of your code that makes the pZiegel identifier reference a new List:

...
            if (pZiegel.isEmpty()) { 
                ziegelHinzufügen = true;
                pZiegel = new List();  // <---- this line
                for (int i = 1; i <= n; i++) {
                    pZiegel.append(i);
                }
            }
...

I believe you are calling the 18th recursion with pZiegel referencing one list (maybe empty). Inside the 18th recursion that line is called and pZiegel starts referencing a new List (realize that the last List still exists and is referenceed by the pZiegiel identifier of the 17th recursion). On the last line of the 18th recursion call you believe you are inspecting the same pZiegiel List from the 17th recursion, but that's not the case.

Upvotes: 1

Related Questions