Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列表的列表数组_C#_Arrays_List - Fatal编程技术网

C# 列表的列表数组

C# 列表的列表数组,c#,arrays,list,C#,Arrays,List,我正在尝试制作一系列的列表,整个事情让我感到困惑。我希望数组更大,所以我做了List[]arr=new List[5],但在添加了一些项目后,我需要通过arr.ElementAt(1).ElementAt(1)[1],但它不应该是另一种方式(开始时是[1]) 我想做的只是填充整个三维,但当我试图通过arr[1].ElementAt(1).添加(…)或arr.ElementAt(1)[1].add(…)(不确定使用哪一个,两个都不起作用)添加最后一个维度时,我收到一个arror,说我正在尝试向空列

我正在尝试制作一系列的列表,整个事情让我感到困惑。我希望数组更大,所以我做了
List[]arr=new List[5]
,但在添加了一些项目后,我需要通过
arr.ElementAt(1).ElementAt(1)[1]
,但它不应该是另一种方式(开始时是
[1]

我想做的只是填充整个三维,但当我试图通过
arr[1].ElementAt(1).添加(…)
arr.ElementAt(1)[1].add(…)
(不确定使用哪一个,两个都不起作用)添加最后一个维度时,我收到一个arror,说我正在尝试向空列表添加一个值

新列表[5]
new List<List<int>>[5]

实际上是一个int列表的列表数组,但您仍然可以在其上调用ElementAt(),因为数组实现了IEnumerable。

在使用它们之前,您需要实例化
列表

arr[0] = new List<List<int>>();
arr[0].Add(new List<int>());
arr[0][0].Add(5);
///etc...
arr[0]=新列表();
arr[0]。添加(新列表());
arr[0][0]。添加(5);
///等等。。。

另一个注意事项:您看到我如何在我的
列表中使用
[]
括号了吗?支持该

下面显示了与所需数据结构的不同交互,以添加和验证元素

var arr = new List<List<int>>[]
    {
        new List<List<int>>()
        {
            new List<int>() { 1, 3, 5 },
            new List<int>() { 2, 4, 6 },
        },
        new List<List<int>>() { new List<int>() },
        new List<List<int>>() { new List<int>() },
        new List<List<int>>() { new List<int>() },
        new List<List<int>>() { new List<int>() },
    };
Assert.AreEqual(4, arr[0].ElementAt(1).ElementAt(1));
Assert.AreEqual(3, arr[0].ElementAt(1).Count);
arr[0].ElementAt(1).Add(8);
Assert.AreEqual(4, arr[0].ElementAt(1).Count);
Assert.AreEqual(8, arr[0].ElementAt(1).ElementAt(3));
var arr=新列表[]
{
新名单()
{
新列表(){1,3,5},
新列表(){2,4,6},
},
新建列表(){new List()},
新建列表(){new List()},
新建列表(){new List()},
新建列表(){new List()},
};
Assert.AreEqual(4,arr[0])。ElementAt(1)。ElementAt(1));
Assert.AreEqual(3,arr[0].ElementAt(1.Count);
arr[0].ElementAt(1).Add(8);
Assert.AreEqual(4,arr[0].ElementAt(1.Count);
Assert.AreEqual(8,arr[0])。ElementAt(1)。ElementAt(3));

ElementAt
也适用于数组。您需要初始化列表。另外,我要么只做
List
要么
int[][]