C# 3 维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17896160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:45:30  来源:igfitidea点击:

C# 3 dimensional array

c#arraysmultidimensional-array

提问by matijakevic

I want to store ARRAY from 3 dimensional array into buildingCostIds, but it says I MUST have 3rd number.

我想将 ARRAY 从 3 维数组存储到 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, *];
}

*I need third number here, but I don't want it because it will extract just number, I want whole array!

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

PROBLEM SOLVED, solution:

问题已解决,解决方法:

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;
}

回答by doctorlove

This might be simpler if you use an array-of-arrays, or jagged array [][][]like

如果您使用的阵列的阵列,或锯齿状排列,这可能是简单的[][][]

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

Then you can access buildingCost[buildingId, 0]which is an array of ints.

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

There is a related question here

有一个相关的问题here



EDIT

编辑

Be aware the "problem solved" you added to the question duplicates the data, so you may run out of memory if you keep doing this. Consider using a List of Lists of Lists and lazy evaluating what you need.

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

回答by WinHtaikAung

Try This

尝试这个

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

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