&#34; List <int> [,]&#34;之间的差异和&#34;列出<list <int>&gt;&#34; </list <int> </int>

时间:2014-03-14 16:49:30

标签: c#

C#中List<int>[,]List<List<int>>"之间的区别是什么?

我知道通话也不同,也可以访问这些职位,但目的是一样的吗?

我已经看过两次具有相同结果的实现,并且已经实现了这两种形式。

3 个答案:

答案 0 :(得分:7)

List<int>[,]是一个二维的列表数组。您应该定义不能更改的“矩阵”大小。创建后,您可以在单元格中添加列表:

List<int>[,] matrix = new List<int>[2, 3]; // size is fixed
matrix[0, 1] = new List<int>(); // assign list to one of cells
matrix[0, 1].Add(42); // modify list items

List<List<int>>是一个列表清单。您有包含其他列表作为项目的列表。它具有单一维度,此维度的大小可以变化 - 您可以添加或删除内部列表:

List<List<int>> listOfLists = new List<List<int>>(); // size is not fixed
listOfLists.Add(new List<int>()); // add list
listOfLists[0].Add(42); // single dimension

它们是不同的数据结构。

实际上,您对List<int>类型的项目过于复杂。结构将与任何类型T保持一致。所以,你有二维数组T[,]和列表List<T>。如上所述,这些数据结构完全不同。

答案 1 :(得分:2)

List<int>[,]是一个包含整数列表的二维数组。

List<List<int>>是整数列表的列表。

所以它们完全不同。常见的是它们都包含整数列表(List<int>),但其中一个是二维数组。其他是整数列表的单个列表。

答案 2 :(得分:0)

List<int>[,]List<int>的二维数组。这意味着它有三个维度(其中2个是固定的)。 List<List<int>>是一个列表列表,因此是2个维度。所以比较它们并没有多大意义。

更好的比较类似于List<int>[] vs List<List<int>>。两者都是整数的2D集合,但第一个是固定的,而后者可以在两个维度上扩展。

List<int>        A collection of integers which will grow as needed
int[]            An array of ints that will have a fixed length
List<int>[]      An array of List<int> which has a fixed number of List<int>, 
                 but each List<int> will expand as needed (by adding ints)
List<List<int>>  A list of list which can grow both by adding more List<int>
                 and by adding ints to one of the List<int>
int[,]           A 2d array of ints that is fixed in both dimensions and rectangular 
                 (i.e. length in both dimensions is always the same)
int[][]          An array of int[], i.e. a jagged array. Fixed in both dimensions but each
                 int[] can have a different length
相关问题