【问题标题】:C# 3 dimensional arrayC# 3 维数组
【发布时间】:2013-07-27 14:20:24
【问题描述】:

我想将 3 维数组中的 ARRAY 存储到 buildingCostIds 中,但它说我必须有第三个数字。

public static int[, ,] buildingCost = { {{0,1,2},{5,5,5}}};

public static void addBuilding(int[] ids, int[] amounts, int buildingId)
{
    int[] buildingCostIds = buildingCost[buildingId, 0, *];
}

*我在这里需要第三个数字,但我不想要它,因为它只会提取数字,我想要整个数组!

问题已解决,解决方案:

public static Array extractArray(int dim1, int dim2)
{
    int[] tempArray = { };

    for (int number=0;number<=2; number++)
    {
    tempArray[number] = buildingCost[dim1, dim2, number];
    }
    return tempArray;
}

【问题讨论】:

  • 为什么问题说C++,标签说C#?
  • 哎呀,第一次来
  • 不用担心 - 修复它做得很好
  • 如果您找到了解决方案,请将相应的答案标记为已接受(即使这意味着您自己在解决方案中添加答案并将其标记为已接受possibly after waiting for a while

标签: c# arrays multidimensional-array


【解决方案1】:

如果您使用数组数组或锯齿数组[][][] 之类的,这可能会更简单

int[][][] buildingCost = new int[2][][];

然后您可以访问buildingCost[buildingId, 0],它是一个整数数组。

有一个相关问题here


编辑

请注意,您添加到问题中的“问题已解决”会重复数据,因此如果您继续这样做,您可能会耗尽内存。 考虑使用 List of Lists 并懒惰地评估您需要的内容。

【讨论】:

  • 不,我有很多数据,我懒得像那样把它们都写下来。找到了解决方案!我会发布它!
【解决方案2】:

试试这个

int[, ,] array3D = new int[3,3,3]

【讨论】:

  • 这对访问最后一列有何帮助?
  • lol Stack Overflow 将其渲染为 trythis 关键字
猜你喜欢
  • 1970-01-01
  • 2021-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多