【问题标题】:Row and column size for a 2-D array二维数组的行和列大小
【发布时间】:2013-10-17 03:51:42
【问题描述】:

下面列出的代码是为了帮助我在 C# 中找到二维数组的行和列大小,但是在访问列长度(GetLength(1))时,我最终得到了 IndexOutOfRangeException。我确实查找了类似的s.o q-a's,但无法确定列大小。

  List<List<int>> intLists = new List<List<int>>();
  for (int i = 0; i < 2; i++)
  {
    List<int> tempList = new List<int>();
    for (int j = 0; j < 5; j++)
      tempList.Add(j + 5+i);
    intLists.Add(tempList);
  }

  int[][] intArray = intLists.Select(Enumerable.ToArray).ToArray();

  Console.WriteLine("Dimension 0 Length = " + intArray[0].Length);
  Console.WriteLine("Dimension 1 Length = " + intArray[1].Length);
  Console.WriteLine("Dimension 0 Length = " + intArray.GetLength(0));
  //Console.WriteLine("Dimension 1 Length = " + intArray.GetLength(1));

【问题讨论】:

    标签: c# arrays c#-4.0


    【解决方案1】:

    intArray 是数组数组,它​​不是二维数组。 Array.GetLength 仅适用于多维数组 (int[,]),因为 intArray[0].Length 可能与 intArray[1].Length 不同

    查看此答案了解更多详情:

    What are the differences between a multidimensional array and an array of arrays in C#?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 1970-01-01
      • 2011-02-08
      • 2020-06-10
      • 1970-01-01
      相关资源
      最近更新 更多