简体   繁体   English

是否可以检查可能是未初始化的变量?

[英]Is it possible to perform checks on what may be an uninitialized variable?

I'm working on a recursive method...我正在研究一种递归方法...

public BinaryTree<T> TreeMirror ( BinaryTree<T> tree ) {
   BinaryTree mirror = new BinaryTree();
   mirror = clone(tree);
   ...
   TreeMirror(...)
   ...
} 

I do not wish for the method to make mirror reference a different BinaryTree object in each recursive step, nor to repeat the mirror = clone(tree) statement after the first iteration.我不希望该方法在每个递归步骤中使mirror引用不同的 BinaryTree BinaryTree ,也不希望在第一次迭代后重复mirror = clone(tree)语句。 I'm wondering if it's possible to put in an if-statement check to see if an instance of mirror has already been initialized -- in which case the mirror = new BinaryTree() and mirror = clone(tree) statements would be skipped.我想知道是否可以进行 if 语句检查以查看mirror的实例是否已经初始化——在这种情况下mirror = new BinaryTree()mirror = clone(tree)语句将被跳过。

I don't think this is possible without passing mirror as an argument into the method or defining it in the class definition... but I want to make sure.如果不将mirror作为参数传递给方法或在 class 定义中定义它,我认为这是不可能的......但我想确定一下。

Any advice is much appreciated.非常感谢任何建议。

---------EDIT----------- - - - - -编辑 - - - - - -

I am not allowed to change the method signature, so I cannot pass the object in in my implementation.我不允许更改方法签名,因此我无法在我的实现中传递 object。 I can create a mirror tree but only by modifying the original tree into a mirror which is something I want to try to avoid.我可以创建镜像树,但只能通过将原始树修改为镜像,这是我想尽量避免的。 I was attempting to create a new BinaryTree object that is the mirror of the original tree that is passed in but really cannot figure out how to do it recursively.我试图创建一个新的 BinaryTree BinaryTree ,它是传入的原始树的镜像,但实际上无法弄清楚如何递归地执行它。

It's rare to see public recursive functions like that.很少看到像这样的公共递归函数。 A better solution might be to have the public method that creates the object, and then calls a private function which is recursive that just makes the necessary changes.更好的解决方案可能是使用创建 object 的公共方法,然后调用私有 function 递归,只进行必要的更改。

It's generally difficult to have the recursive function signature match what you want to show to your clients.通常很难让递归 function 签名与您想向客户展示的内容相匹配。

The mirror variable is local to the method, and will ALWAYS be unitialized in each call.镜像变量是方法的本地变量,并且总是在每次调用中都被初始化。

Passing mirror as an argument to the method is a very good option.将 mirror 作为参数传递给该方法是一个非常好的选择。

EDIT: If you can't modify the method signature, can you create a private method and call that to perform the recursion?编辑:如果你不能修改方法签名,你可以创建一个私有方法并调用它来执行递归吗?

"I don't think this is possible without passing mirror as an argument into the method or defining it in the class definition... but I want to make sure." “如果不将 mirror 作为参数传递给方法或在 class 定义中定义它,我认为这是不可能的……但我想确定一下。”

Correct, that would be one way to do what you want, as mirror is not a recursion invariant.正确,这将是做你想做的事情的一种方法,因为镜像不是递归不变量。

The other way would be that your recursive algorithm clones just nodes, not entire subtrees.另一种方法是您的递归算法仅克隆节点,而不是整个子树。

Uri's answer is the best.... refactor it into a private method and simply initialize the mirror then invoke the private (recrsive) method passing mirror as a parameter. Uri 的答案是最好的......将其重构为私有方法并简单地初始化镜像,然后调用私有(recrsive)方法将镜像作为参数传递。

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

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