【问题标题】:Convert a for loop to a recursive method java将for循环转换为递归方法java
【发布时间】:2017-10-26 04:49:43
【问题描述】:

我正在尝试将此循环转换为递归方法。
这个迭代版本有效:(之后递归尝试)

        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++;
            }
        }

我的递归尝试:

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

标签: java loops for-loop recursion


【解决方案1】:

基本问题

它们不会“回到原来的状态”——当您退出方法实例时,这些变量会被销毁(释放回内存堆)。

这些是本地变量——当你进入方法时分配,退出时释放;有些是输入参数,并从调用的参数接收它们的初始值。因此,他们不会“回到原来的状态”。您看到的是该方法的前一个实例中未更改的状态。

每次调用例程时,都会将另一个实例添加到堆栈中。每个实例都有自己的变量版本,它们恰好共享名称。他们确实分享价值观。

解决方案

您可以通过遵循模块化编程的基本原则来解决此问题:您将值传递给带有参数列表的方法;您可以通过 return 列表获取值。例如,您最后的 else 子句可能看起来像这样:

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

没有阅读您的代码以获得功能;您可能需要tools.add(child_result) 以外的其他东西来累积正确的值。不过,我相信我已经告诉了你目前给你带来麻烦的基本原则。

【讨论】:

  • 谢谢你,我希望我在解决我的方法之前有这个,但现在我没有足够的力量再次改变数百行^^无论如何我下次会记住这个谢谢
  • 这就是万物之主发明全局更改命令和编辑器宏的原因。 :-)
  • 那些是什么?他们似乎很有帮助
  • 查看您的文本编辑器的文档。全局更改命令对整个文件进行更改,所有出现。宏可让您存储由单个命令键入的命令序列 - 甚至是一次击键。
【解决方案2】:

问题来自指针,我用作参数的变量仅针对方法保持更改,并在结束时恢复到原来的状态。我通过将变量设置为静态类变量并在需要时重置它们来绕过这一点,但是来自 Prune 的解决方案看起来更好且不那么混乱。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 2017-02-28
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 2015-01-08
    相关资源
    最近更新 更多