递归地添加到列表中的列表

时间:2015-02-16 13:48:07

标签: c# recursion

我想创建一个递归函数,无论基于多维数组的长度如何,它都将添加到下面列出的nestedContainer对象中。根将为零,只要您从根工作,您可以根据需要添加。因此0将包含0.0, 0.1, 0.20.0将包含0.0.0, 0.0.1, 0.0.2,依此类推。必须在孩子存在之前创建父母。

    public class Container()
    {
         public List<Container> container {get; set;}
         public string containerName {get; set;}
         public string index {get; set;}
    } 

    public class ContainerBuilder()
    {
         //This is the main container that will contain all of the children
         Container nestedContainer = new Container();

         //This method will take in the parent index value and then will add the new container into the parent container list based on the value specified
         public void AddContainer(string parentIndex, string containerName, string index)
         {
              Container container = new Container() 
              {
                 index = index,
                 name = name,
                 container = new List<Container>()
              }

              SetContainer(parentIndex, index, container);
          }

          private void SetContainer(string, parentIndex, string index, Container container)
          {
              //Recurive function that will add the new container in the parent container working its way back from the parent   

             //Get the root container, a starting point to add the children
             var rootContainer = nestedContainer .contains[int.Parse(parentIndex.Split('.')[0])];   
          }
     }

实施

ContainerBuilder builder = new ContainerBuilder();

builder.AddContainer("0", "Parent 0", "0"); 
builder.AddContainer("0", "Child of parent", "0.x"); 
builder.AddContainer("0.x", "child of child", "0.x.x"); 
builder.AddContainer("0.x.x", "child of a child of a child", "0.x.x.x"); 

只要路径匹配,您就会看到索引应该是不可靠的,所以无论您采用哪种方式,都可以添加任意数量的子项。

1 个答案:

答案 0 :(得分:2)

每个Container都需要知道其父级。顶级父级将父级设置为null。每个容器还有一个Container

列表
public class Container
{
    private Container _parent = null;

    public Container(Container parent, int index)
    {
        _parent = parent;
        Containers = new List<Container>();
        Index = index;
    }

    public List<Container> Containers { get; set; }
    public string ContainerName { get; set; }
    public string Index { get; set; }
}

然后你可以向两个方向递归

编辑:这里有几个要添加到Container课程的函数,我认为可能会让你开始:

//Returns the path of the this container prepending all parent indeces
public string GetPath()
{
    string ret = "";
    if (_parent != null)
    {
        ret = _parent.GetPath();
        ret += String.Format("{0}.", Index);
    }
    return ret;
}

//Gets a child ensuring all container lists contain enough elements
public Container GetChild(string indexPath)
{
    string[] pathParts = indexPath.Split(new[] { '.' }, 2);
    if (pathParts.Any())
    {
        int index;
        if (int.TryParse(pathParts[0], out index))
        {
            //make sure there's enough containers
            Containers = Enumerable.Range(0, index +1).Select(i => new Container(this,i)).ToList();
            if (pathParts.Count() == 2)
            {
                //more sub children so recursively add...
                return Containers[index].GetChild(pathParts[1]);
            }
            return Containers[index];
        }
    }
    return null;
}

经过测试:

Container c = new Container(null,0);
Console.WriteLine(c.GetChild("2.2.2").GetPath());
Console.WriteLine(c.Containers[0].GetPath());
Console.WriteLine(c.Containers[1].GetPath());
Console.WriteLine(c.Containers[2].GetPath());
Console.WriteLine(c.Containers[2].Containers[0].GetPath());
Console.WriteLine(c.Containers[2].Containers[1].GetPath());
Console.WriteLine(c.Containers[2].Containers[2].GetPath());
Console.WriteLine(c.Containers[2].Containers[2].Containers[0].GetPath());
Console.WriteLine(c.Containers[2].Containers[2].Containers[1].GetPath());
Console.WriteLine(c.Containers[2].Containers[2].Containers[2].GetPath());

哪个输出

2.2.2.
0.
1.
2.
2.0.
2.1.
2.2.
2.2.0.
2.2.1.
2.2.2.

只需删除最后一次&#34;。&#34;