简体   繁体   English

二维递归始终返回0

[英]2-D Recursion Always Returns 0

Ok, so this is an addendum to my question here , which was answered. 好了,这是一个增编我的问题在这里 ,这是回答。

Not wanting to cheapen the answer for the previous question, I decided this should have its own question in fairness to JB Nizet who answered my first question, and received credit for such. 我不想回答上一个问题的答案,我认为这对于回答我的第一个问题的JB Nizet公平性应该有自己的问题,并且因此获得了赞誉。

I am implementing a 2-dimensional data structure for a class.The approach used is an "NxN" Array of objects. 我正在为一个类实现一个二维数据结构。使用的方法是一个“NxN”对象数组。

So, 所以,

Cell[][] dataStructure = new Cell[N][N];

Each cell relies on the output of the cell to the left of it, and to the cell above it, to create its own output. 每个单元依赖于其左侧的单元格输出,并依赖于其上方的单元格,以创建自己的输出。 The 2-d structure will be tested for a range of inputs, 000 to 111. 2-d结构将测试一系列输入,000到111。

Example of 2-D structure and how the outputs flow into the next cells 2-D结构的示例以及输出如何流入下一个单元

在此输入图像描述

Example: 例:

Assuming standard X,Y directions, I attempt to get the output of the bottom right cell using the following method: 假设标准的X,Y方向,我尝试使用以下方法获取右下方单元格的输出:

/**
 * Recursive method that returns the output of a given cell
 * @param row: the row the cell is in (its yPos)
 * @param inputs:
 * @param column: the column the  cell is in (its xPos)
 */
private int[] getOutput(int[] inputs,int yPos, int xPos){
        if (yPos==-1){
              int[] out = new int[2];
              out[0] = 0;  // yPos
              out[1] = inputs[xPos];  //xPos
              return out;
            }
        else if (xPos==-1){
              int[] out = new int[2];
              out[0] = inputs[yPos];  //yPos
              out[1] = 0;  //xPos
              return out;
        }

        int[] leftOutput = getOutput(inputs, yPos, xPos-1);
        int[] topOutput = getOutput(inputs, yPos-1, xPos);

         return currentStructure[yPos][xPos].getResult(leftOutput[1], topOutput[0]);
}

To simplify things, I now have a single getResult method that for a cell in the 2-d structure, performs logic on the specified inputs. 为简化起见,我现在有一个getResult方法,对于二维结构中的单元格,在指定的输入上执行逻辑。 The result is an int[] of two outputs, one for each direction. 结果是两个输出的int [],每个方向一个。

The getResult method, as it currently stands is written as such: 目前的getResult方法是这样编写的:

    public int[] getResult(int left, int top)
{
    int[] resultOut = new int[2];
    if (xDirStr.equals("00")){ // AND
        resultOut[0]= left * top;
    }
    if (xDirStr.equals("01")){ // OR
        if (left ==1 || top ==1)
               resultOut[0]= 1;
        else
       resultOut[0] =0;;
    }
    if (xDirStr.equals("10")){ // NOT, USES ONLY NOT X
        if (left ==0)
       resultOut[0]= 1;
        else
        resultOut[0]= 0;
    }
    if (xDirStr.equals("11")){ // XOR
        if ( (left==1 && top==0) || (left==0 && top==1))
            resultOut[0]= 1;
        else
        resultOut[0]= 0;
    }

    if (yDirStr.equals("00")){ // AND
        resultOut[1]= left * top;
    }
    if (yDirStr.equals("01")){ // OR
        if (left ==1 || top ==1)
               resultOut[1]= 1;
        else
        resultOut[1]= 0;
    }
    if (yDirStr.equals("10")) { // NOT, USES ONLY NOT X
        if (left ==0)
        resultOut[1]= 1;
        else
        resultOut[1]= 0;
    }
    if (yDirStr.equals("11")) { // XOR
        if ( (left==1 && top==0) || (left==0 && top==1))
            resultOut[1]= 1;
        else
        resultOut[1]= 0;
    }
        return resultOut;
}

I've debugged step-by-step but can't catch my issue. 我已逐步调试,但无法解决我的问题。 The output does not match what I recreate by hand to verify it, the outputs are always 0. Any helpful hints would be appreciated! 输出与我手工重新创建的不一致,以验证它,输出始终为0.任何有用的提示将不胜感激!

My specific question, is why does getOutput always return 0? 我的具体问题是,为什么getOutput总是返回0? From what I see debugging, the issue is not in my logic application, which I've not included here. 从我看到的调试,问题不在我的逻辑应用程序中,我没有包含在这里。

Thanks again. 再次感谢。

=======UPDATE======== A sample 3x3, at the request of BevynQ. =======更新========根据BevynQ的要求,样本3x3。

The inputs, along the left and top edges change during each test, this example is for the base case 000. **The NOT function ALWAYS returns the logical NOT of the value entering from the left.* The "Output" I'm attempting to check in my methods is circled in red. 在每次测试期间,左边和上边缘的输入都会发生变化,此示例适用于基本情况000. ** NOT函数始终返回从左边输入的值的逻辑NOT。*“输出”我正在尝试检查我的方法是用红色圈出来的。

000的3x3示例

Eureka, it was so simple that I overlooked it and didn't catch it. 尤里卡,它是如此简单,我忽略了它,并没有抓住它。 The problem is mostly related to my poor choice of variable names. 问题主要与我对变量名称的选择不当有关。 I tinkered with maybe having the wrong array outputs, but the solution was to change the return line to: 我修补了可能有错误的数组输出,但解决方案是将返回线更改为:

     return currentStructure[xPos][yPos].getResult(leftOutput[1], topOutput[0]);

See the difference? 看到不同? I swapped xPos and yPos. 我交换了xPos和yPos。 I understood for a brief shining moment while debugging, but the explanation of it is lost in my brain again. 我在调试时理解了一个短暂的闪光时刻,但它的解释再次在我的大脑中消失了。 It's related to accessing the structure, which requires the yPosition first, then the xPosition... which is the opposite of the standard (xy) notation we all know and love. 它与访问结构有关,首先需要yPosition,然后是xPosition ......这与我们都知道和喜欢的标准(xy)符号相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM