C#3维数组 [英] C# 3 dimensional array

查看:66
本文介绍了C#3维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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


推荐答案

如果您使用数组数组或锯齿状数组 [] [] []

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

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

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

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

有一个相关问题此处

编辑

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

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.

这篇关于C#3维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆