2017-05-25 315 views
1

我试图将此循环转换为递归方法。
这种迭代版本的作品:(递归操作来之后)将for循环转换为递归方法java

 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; 

     } 

我不知道发生了什么事错了。我试图从迭代版本复制所有功能,但我无法实现它的功能。 我在做什么错?

+0

编辑:刚刚发现,方法结束时,我在方法中改变的值会回到原来的状态,这可能是问题,任何人都知道如何绕过这个问题? – dfghdf89

+0

你的基本情况是什么?换句话说,递归会停止的条件是什么? –

+0

当我 dfghdf89

回答

1

基本问题

他们不会“回到自己的老态” - 这些变量被销毁(释放回内存堆),当您退出方法实例。

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

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

SOLUTION

您通过保持模块化编程的基本原则,解决此问题:您传递值与参数列表的方法;您可以使用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)以外的东西来积累适当的值。不过,我相信我已经给了你目前给你带来麻烦的基本原则。

+0

谢谢你的男人我希望我在解决这个问题之前有过这个,但是现在我没有力量去改变数百个行再次^^ 无论如何,我会记住这下次谢谢 – dfghdf89

+0

这就是为什么所有创造者发明了全局变化命令和编辑器宏。 :-) – Prune

+0

那些是什么?他们似乎很有帮助 – dfghdf89

0

问题来自指针,我用作参数的变量只是在方法中保持不变,并在结束时回到原来的状态。我通过将变量设置为静态类变量并在需要时重置它们来绕过此问题,但来自Prune的解决方案看起来更好,更不麻烦。