【问题标题】:Find length of specific dimension of 2D list查找二维列表特定维度的长度
【发布时间】:2015-01-02 23:18:04
【问题描述】:

我需要在以下循环之前检查二维整数列表“centreX1”的第一维长度:

  for (x = 0; x < (int)centreX1[0].Count(); x++)
            {
                if (BinarySpotsInsideTolerance1[0][x] == 1)
                {
                    AllspotsY.Add(centreY1[0][x]);
                    AllspotsX.Add(centreX1[0][x]);
                    AllspotsRLU.Add(RLUSpotsthreshold1[0][x]);
                }
            }

如果 centerX1 没有成员,则在 centerX1[0].Count() 处引发错误。

【问题讨论】:

  • 在运行代码之前检查centreX1.Count &gt; 0
  • 如果是数组,那么你的答案就在你的问题标题中
  • 不是数组 - 整数列表的列表

标签: c# list multidimensional-array count 2d


【解决方案1】:

如果centreX1 没有元素,则无法计算centreX1[0] 中的元素数量。

确保centreX1 中包含元素,然后再尝试访问第一个元素。

if (centreX1.Any())  // or "if (centreX1.Count() > 0)"
{
    for (x = 0; x < (int)centreX1[0].Count(); x++)
    {
        if (BinarySpotsInsideTolerance1[0][x] == 1)
        {
            AllspotsY.Add(centreY1[0][x]);
            AllspotsX.Add(centreX1[0][x]);
            AllspotsRLU.Add(RLUSpotsthreshold1[0][x]);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-29
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2017-01-22
    • 1970-01-01
    相关资源
    最近更新 更多