如何递归遍历嵌套泛型集合?

时间:2014-09-09 09:59:51

标签: c# .net generics recursion icollection

我想计算嵌套集合中类的数量,例如[[[1,2],[3,4,5]],[[1,2],[3,4,5]]]。这里引用的类是" int"预期的答案是10。

我将此列表生成为:

        List<int> list1 = new List<int>(2);
        list1.Add(1);
        list1.Add(2);
        List<int> list2 = new List<int>(3);
        list2.Add(3);
        list2.Add(4);
        list2.Add(5);

        List<List<int>> listlist = new List<List<int>>(2);
        listlist.Add(list1);
        listlist.Add(list2);

        List<List<List<int>>> listlistlist = new List<List<List<int>>>(2);
        listlistlist.Add(listlist);
        listlistlist.Add(listlist);

当我编程时,我更喜欢为泛型类编写这样的方法,我的代码是:

    public static int CountT<T>(ICollection<T> pTCol, int intLevel = 1)
    {
        int intCount = 0;
        if (intLevel > 0)
        {
            intLevel--;
            foreach (T pT in pTCol)
            {
                ICollection<T> subTCol = pT as ICollection<T>;   //PROBLEM: I got "null" for subTCol when the program was running
                intCount += CountT(subTCol, intLevel);
            }
        }
        else if (intLevel == 0)
        {
            intCount = pTCol.Count;
        }
        return intCount;
    }

我通过

测试了上面的代码
        int intCount = CountT(listlistlist, 2);

然后我遇到了问题

        ICollection<T> subTCol = pT as ICollection<T>;   //PROBLEM: I got "null" for subTCol when the program was running

我也试过了代码:

    public static int CountT2<T, T2>(ICollection<T> pTCol, int intLevel = 1)
    {
        int intCount = 0;
        if (intLevel > 0)
        {
            intLevel--;
            foreach (T pT in pTCol)
            {
                ICollection<T2> subTCol = pT as ICollection<T2>;
                intCount += CountT2(subTCol, intLevel);   //PROBLEM: The type arguments for method cannot be inferred from the usage. Try specifying the type arguments explicitly. (I could not pass the compiling)
            }
        }
        else if (intLevel == 0)
        {
            intCount = pTCol.Count;
        }
        return intCount;
    }

我无法通过编译

        intCount += CountT2(subTCol, intLevel);   //PROBLEM: The type arguments for method cannot be inferred from the usage. Try specifying the type arguments explicitly. (I could not pass the compiling)

我该怎么做?

1 个答案:

答案 0 :(得分:1)

ICollection subTCol = pT as ICollection //问题:对于subTCol我得到了“null”

这是空的原因是pT不是ICollection<T>,因为TintlistlistlistList<List<List<T>>>。因此,pTList<List<T>>,这就是为什么如果您尝试将其转换为ICollection<T>,您将获得null。

我想计算嵌套集合中的项目数。

使用Sum方法可以更轻松地完成这项工作。如果您知道嵌套级别,例如:

Assert.AreEqual(10, listlistlist.Sum(x => x.Sum(y => y.Count)));

如果你不知道嵌套级别或者你想要一个更通用的方法,你可以创建一个扩展方法,如:

public static class RecursiveSumExtension
{
    public static int RecursiveSum(this IEnumerable items)
    {
        if (null == items)
            return 0;

        var total = 0;

        foreach (var item in items)
        {
            if (item is IEnumerable)
                total += (item as IEnumerable).RecursiveSum();

            else
                total++;
        }

        return total;
    }
}

和测试:

 Assert.AreEqual(10, listlistlist.RecursiveSum());

列表生成

除此之外,您可以使用的集合初始化程序语法在创建集合时使代码更具可读性:

        var listlist = new List<List<int>>
        {
            new List<int> {1, 2},
            new List<int> {3, 4, 5}
        };

        var listlistlist = new List<List<List<int>>>
        {
            listlist,
            listlist
        };