博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个循环递归遍历问题
阅读量:4494 次
发布时间:2019-06-08

本文共 1011 字,大约阅读时间需要 3 分钟。

数据结构模型如下:

public Class A{    public string Name{
get;set;} public list children{
get;set;}}

现要寻找一个子节点,目前代码如下:

public A FindByName(string _name)        {            if (this.Name == _name)                return this;            else            {                for (int i = 0; i < this.children.Count; i++)                                     return this.children[i].FindByName(_name);            }            return null;        }

但是,VS编译器一直提示 i++是 "Unreachable code detected".

其运行结果也相差很多,这个循环只遍历了第0个孩子,孙子。。。

现正确的做法如下:

public A FindByName(string _name)        {            if (this.Name == _name)                return this;            else            {                A p = null;                for (int i = 0; i < this.children.Count; i++)                {                    p = this.children[i].FindByName(_name);                    if (p != null)                        break;                }                return p;            }        }

 

转载于:https://www.cnblogs.com/crazyghostvon/p/3568659.html

你可能感兴趣的文章
OpenGL的矩阵
查看>>
一天 zepto
查看>>
知乎趣闻
查看>>
centos使用--supervisor使用
查看>>
HDU5449 Robot Dog
查看>>
向div添加圆角边框
查看>>
B、B*、B+
查看>>
Markdown
查看>>
mvc4 @Html.DropDownList
查看>>
CentOS7中rpm,yum软件安装命令
查看>>
第一课 C语言简明教程
查看>>
洛谷1004方格取数
查看>>
洛谷1297[国家集训队]单选错位
查看>>
Python基础 collections模块
查看>>
Python实现各种排序算法的代码示例总结
查看>>
Android入门(一):创建Android工程
查看>>
HTTP 304
查看>>
Python_base_函数返回值
查看>>
iOS项目 -- 模仿花椒直播做的第一层次界面
查看>>
关于string的replace方法
查看>>