Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将for循环转换为递归方法java_Java_Loops_For Loop_Recursion - Fatal编程技术网

将for循环转换为递归方法java

将for循环转换为递归方法java,java,loops,for-loop,recursion,Java,Loops,For Loop,Recursion,我正在尝试将此循环转换为递归方法。 这个迭代版本有效:(递归尝试之后出现) public static int subcompareTowcol(作业j1、int index1、int index2、int indexTab1){//几乎与上面的方法相同,但在遇到0时会进行修改。 int indexData=0; int cost=j1.作业[index1].长度; int nZeros=numberCounter(j1.jobS[index2],0); //下使用的索引,以避免执行过多的操作。

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

public static int subcompareTowcol(作业j1、int index1、int index2、int indexTab1){//几乎与上面的方法相同,但在遇到0时会进行修改。
int indexData=0;
int cost=j1.作业[index1].长度;
int nZeros=numberCounter(j1.jobS[index2],0);
//下使用的索引,以避免执行过多的操作。
int tab1Index=0;

而(indexData基本问题

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

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

每次调用例程时,都会向堆栈中添加另一个实例。每个实例都有自己版本的变量,这些变量恰好共享名称。它们不共享值

解决方案

通过遵循模块化编程的基本原则来解决这个问题:使用参数列表将值传递到方法中;使用
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\u result)
来累积正确的值。但是,我相信我已经给了你目前给你带来麻烦的基本原理。

问题来自指针,我用作参数的变量只是在方法中保持更改,并在结束时返回到原来的状态。我通过将变量设置为stati绕过了这一点c类变量,并在需要时重置它们,但Prune的解决方案看起来更好,也不那么凌乱。

Edit:刚刚发现我在方法中更改的值在方法结束时返回到其旧状态,这可能是问题所在,有人知道如何绕过它吗?基本情况是什么?或者换句话说,wh中的条件是什么ich递归将在ipublic 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; }
child_result = recurs(j1, index1, index2, indexTab1,
                      tab1, tab2, tab3, nZeros,
                      cost, i, tab1Index, b);
tools.add(child_result);
return tools;