Recursive List Flattening

后端 未结 13 1330
既然无缘
既然无缘 2020-11-27 04:25

I could probably write this myself, but the specific way I\'m trying to accomplish it is throwing me off. I\'m trying to write a generic extension method similar to the oth

13条回答
  •  醉梦人生
    2020-11-27 05:12

    Basicly, you need to have a master IENumerable that is outside of your recursive function, then in your recursive function (Psuedo-code)

    private void flattenList(IEnumerable list)
    {
        foreach (T item in list)
        {
            masterList.Add(item);
    
            if (item.Count > 0)
            {
                this.flattenList(item);
            }
        }
    }
    

    Though I'm really not sure what you mean by IEnumerable nested in an IEnumerable...whats within that? How many levels of nesting? Whats the final type? obviously my code isn't correct, but I hope it gets you thinking.

提交回复
热议问题