简体   繁体   English

如何制作列表的可枚举范围列表?

[英]How to make an Enumerable Range List of Lists?

I can make a List of chars like this: 我可以这样列出一个chars列表:

List<char> letter = Enumerable.Range(0, 10)
                              .Select(i => '\0')
                              .ToList();

And a List of int? 还有一个int?列表int? like this: 像这样:

List<int?> number = Enumerable.Range(0, 10)
                              .Select(i => (int?)i)
                              .ToList();

And call them by letter[1] = 'a' and number[1] = 5 . 并用letter[1] = 'a'number[1] = 5称呼它们。


How can I make a List (or HashSet ) of 10 List<char> 's? 如何制作10个List<char>List (或HashSet )?

Something like: 就像是:

List<char> myList = Enumerable.Range(0, 10)
                              .Select(i => List<char> i)
                              .ToList();

myList[1] , myList[2] , myList[3] myList[1]myList[2]myList[3]


I want to loop through and add items to each list. 我想遍历并将项目添加到每个列表。

for (int i = 0; i < 10; i++)
{
    myList[i].Add(letter[i]);
}

You can combine your two approaches as follows: 您可以将两种方法结合使用,如下所示:

List<List<char>> myList = Enumerable.Range(0, 10)
     .Select(i => Enumerable.Range(0, 10).Select(c => '\0').ToList())
     .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM