代码之家  ›  专栏  ›  技术社区  ›  André Miranda

找到我的下属的下属等等

  •  1
  • André Miranda  · 技术社区  · 16 年前

    我本来想完成的,但是找不到第二级下属。。。

    谢谢!!

    5 回复  |  直到 16 年前
        1
  •  0
  •   Nix    16 年前

    PSUDO公司:

      private List<Subordinate> GetSubordinates(Subordinate you){
            List<Subordinate> subs = new List<Subordinate>();
            if(!you.HasSubordinates){
                  return subs;
             }
    
             foreach(Subordinate s in you.Subordinates){
                subs.AddRange(GetSubordinates(s));
            }
      }
    
        2
  •  3
  •   user1228 user1228    16 年前
    private IEnumerable<Employee> RecursiveGet(Employee durr)
    {
      foreach(var sub in durr.Subordinates)
      {
        yield return sub;
        foreach(var recurse in RecursiveGet(sub))
          yield return recurse;
      }
    }
    
        3
  •  1
  •   Prutswonder    16 年前

    你试过用吗 recursion

        4
  •  0
  •   Datoon    16 年前

    像下面这样?

    object RecursiveCall(Collection collection, object itemToFind)
    {
        foreach(var item in collection)
        {
            if(item == itemToFind)
            {
                 return item;
            }
            else
            {
                 RecursiveCall(item, itemToFind);
            }
        }
    }
    
        5
  •  0
  •   Kangkan    16 年前

    为此,请使用递归技术的实现。