如何填充数组结构列表的复杂数组c#?

时间:2015-02-23 18:42:55

标签: c# arrays list

我很难尝试填充这个结构:

var arrlist = new List<int[]>()[length];

我想要的是:一组固定长度的列表。 每个列表将包含长度为2的未知数量的数组。

尝试将以下列表添加到上面的数组中:

var digfaclist = new List<int[]>();
var factors = new int[2] {i, j};
digfaclist.Add(factors);

我循环遍历arrlist并在列表中填充长度为2的数组。在那之后,我试图做像:

arrlist[0] = digfaclist;

它是否可行,或者我应该采取其他方法?性能/结构,副

2 个答案:

答案 0 :(得分:0)

这样的事情?

int count = 15;


List<int[]>[] list = new List<int[]>[count];


for (int i = 0; i < count; i++)
{
    list[i] = new List<int[]>(); //you have to initialize them
}
list[0].Add(new []{30, 22});
list[0].Add(new []{11, 22});
foreach (int[] numbers in list[0])
{
   Console.WriteLine(string.Join(",",numbers)); //test
}

答案 1 :(得分:-1)

这是你在找什么?

var arr = new int[][] {
  new int[] { 1, 2 },
  new int[] { 3 ,4 },
  new int[] { 5, 6 } }
var arrlist = arr.ToList();

在内部记住,列表仍然保留一系列项目。