扶强 2019-02-25 14:57 采纳率: 100%
浏览 326
已采纳

将以下代码改成递归形式

public void Calc1(int initCount, int avgCount, int endClass)
        {
            List<TreeData> list1 = new List<TreeData>();
            TreeData data1 = new Models.TreeData() { id = 0, name = "推客", parentid = 0, value = 0 };
            List<TreeData> list2 = new List<Models.TreeData>();
            for (int b = 0; b < avgCount; b++)
            {
                TreeData data2 = new Models.TreeData() { id = 0, name = "推客", parentid = 0, value = 0 };
                List<TreeData> list3 = new List<Models.TreeData>();
                for (int c = 0; c < avgCount; c++)
                {
                    TreeData data3 = new Models.TreeData() { id = 0, name = "推客", parentid = 0, value = 0 };

                    list3.Add(data3);
                    data2.children = list3;
                }
                list2.Add(data2);
                data1.children = list2;
            }
            list1.Add(data1);
        }

TreeData 结构如下

 public class TreeData
    {
        public string name { get; set; }

        public int? value { get; set; }

        [JsonIgnore]
        public int? id { get; set; }
        [JsonIgnore]
        public int? parentid { get; set; }

        public List<TreeData> children { get; set; }
    }

最终层级是endClass

  • 写回答

1条回答 默认 最新

  • 起个昵称真费劲 2019-02-25 15:51
    关注
            public void Calc1(int initCount, int avgCount, int endClass)
            {
                List<TreeData> list1 = new List<TreeData>();
                int index = 1;
                for (int i = 0; i < initCount; i++)
                {
                    TreeData data1 = new TreeData() { id = 0, name = "推客", parentid = 0, value = 0, index = index };
                    data1.children = new TreeData().GetTreeData(avgCount, endClass, index);
                    list1.Add(data1);
                }
            }
    
            public class TreeData
            {
                //当前深度
                public int index { get; set; }
    
                public string name { get; set; }
    
                public int? value { get; set; }
    
    
                public int? id { get; set; }
    
                public int? parentid { get; set; }
    
                public List<TreeData> children { get; set; }
    
    
                public List<TreeData> GetTreeData(int avgCount, int endClass, int index)
                {
                    List<TreeData> list = new List<TreeData>();
                    index++;
                    this.index = index;
                    for (int i = 0; i < avgCount; i++)
                    {
                        TreeData data1 = new TreeData() { id = 0, name = "第" + index + "级", parentid = 0, value = 0, index = index };
                        if (this.index != endClass)
                        {
                            data1.children = new TreeData().GetTreeData(avgCount, endClass, index);
                        }
                        list.Add(data1);
                    }
    
                    return list;
                }
            }
    
    
        //调用 - by 没想好
        Calc1(2,3,7);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥15 抖音看过的视频,缓存在哪个文件