dfghdf89
dfghdf89

Reputation: 11

Convert a for loop to a recursive method java

I'm trying to convert this loop to a recursive method.
This iterative version works: (recursive attempt comes after that)

        public static int subCompareTwoCol(JobS j1, int index1, int index2, int indexTab1){ //Almost same method as above but with modifications when it encounters a 0.
        int indexData = 0;                                              
        int cost = j1.jobS[index1].length;                                       
        int nZeros = numberCounter(j1.jobS[index2], 0); 

        //index used under to not do the operations too many times.
        int tab1Index = 0;


        while(indexData<j1.jobS[index1].length){

            if(j1.jobS[index1][indexData] != 0){
                    assert(index2<6);
                    int j = numberCounter(j1.jobS[index1], j1.jobS[index1][indexData]);
                    int k = numberCounter(j1.jobS[index2], j1.jobS[index1][indexData]);

                        if(j <= k){
                            cost-=j;
                        }
                        if(j > k){
                            cost-=k;
                        }

                    indexData += j;

            }else{

                //Initialize 3 tabs, stocks them (previous column, actual column and next column).
                int[] tab1 = j1.jobS[indexTab1];
                int[] tab2 = j1.jobS[index1];
                int[] tab3 = j1.jobS[index2];

                //I want to convert this part !
                //For every numbers from the first tab (starts at the index so it doesnt count the same tool twice if multiple 0's).
                for(int i=tab1Index; i<Gui.toolN; i++){
                    tab1Index++;
                    if(isIn(tab3, tab1[i]) == true && isIn(tab2, tab1[i]) == false){ 
                        //Modifications to cost (and -=1 to nzeros because we "change" a 0 to the new tool).
                        nZeros-=1;
                        cost -=2;
                        tools.add(tab1[i]);

                        break;

                    }
                }
//This is what i think the code would look for replacing the for loop.
//                  int i=0;
//                  boolean b = false;
//                  assert(tab2[indexData]==0);
//                  recurs(j1, index1, index2, indexTab1, tab1, tab2, tab3, nZeros, cost, i, tab1Index, b);
//                  i=0;
                indexData++;
            }
        }

My attempt at recursion:

public static void recurs(JobS j1, int index1, int index2, int indexTab1, int[] tab1, int[] tab2, int[] tab3, int nZeros, int cost, int i, int tab1Index, boolean b){ //j1 is the input JobS, start b is false, start i with 0.

        i=Integer.valueOf(tab1Index);
        if(i<Gui.toolN){
            tab1Index++;


            if(isIn(tab3, tab1[i]) == true && isIn(tab2, tab1[i]) == false){ 
                //Modifications to cost (and -=1 to nzeros because we "change" a 0 to the new tool).
                nZeros-=1;
                cost -=2;
                tools.add(tab1[i]);

                return; 
            }else{
                i++;
                recurs(j1, index1, index2, indexTab1, tab1, tab2, tab3, nZeros, cost, i, tab1Index, b);
            }
            return; 

        }

I've no idea what's going wrong. I've tried to replicate all functionality from the iterative version, but I just can't get it to work. What am I doing wrong?

Upvotes: 1

Views: 148

Answers (2)

dfghdf89
dfghdf89

Reputation: 11

The problem was coming from pointers, the variables i used as parameters only stayed changed for the method and came back to their old state when it ended. I bypassed this by setting the variable as static class variables, and resetting them when i wanted to, but the solution from Prune looks better and less messy.

Upvotes: 0

Prune
Prune

Reputation: 77847

BASIC PROBLEM

They don't "go back to their old state" -- those variables are destroyed (released back to the memory heap) when you exit the method instance.

Those are local variables -- allocated when you enter the method, released on exit; some are input parameters, and receive their initial values from the arguments to the call. Thus, they don't "go back to their old state". What you're seeing is the unchanged states from the previous instance of the method.

Each time you call the routine, you add another instance to the stack. Each instance has its own version of the variables, which happen to share names. They do not share values.

SOLUTION

You work around this by keeping to basic principles of modular programming: you pass values into a method with the parameter list; you get values back with the return list. For instance, your final else clause might look something like this:

child_result = recurs(j1, index1, index2, indexTab1,
                      tab1, tab2, tab3, nZeros,
                      cost, i, tab1Index, b);
tools.add(child_result);
return tools;

I have not read your code for functionality; you may well need something other than tools.add(child_result) to accumulate the proper values. However, I believe that I've given you the basic principle that's currently giving you trouble.

Upvotes: 1

Related Questions