初始化锯齿状数组

时间:2009-11-15 21:58:08

标签: c# .net jagged-arrays

我想在C#中创建数组10 * 10 * 10,例如int[][][](不是int[,,])。

我可以写代码:

int[][][] count = new int[10][][];
for (int i = 0; i < 10; i++) {
    count[i] = new int[10][];
    for (int j = 0; j < 10; j++)
        count[i][j] = new int[10];
}

但我正在寻找更美妙的方式。可能是这样的:

int[][][] count = new int[10][10][10];

9 个答案:

答案 0 :(得分:22)

int[][][] my3DArray = CreateJaggedArray<int[][][]>(1, 2, 3);
使用

static T CreateJaggedArray<T>(params int[] lengths)
{
    return (T)InitializeJaggedArray(typeof(T).GetElementType(), 0, lengths);
}

static object InitializeJaggedArray(Type type, int index, int[] lengths)
{
    Array array = Array.CreateInstance(type, lengths[index]);
    Type elementType = type.GetElementType();

    if (elementType != null)
    {
        for (int i = 0; i < lengths[index]; i++)
        {
            array.SetValue(
                InitializeJaggedArray(elementType, index + 1, lengths), i);
        }
    }

    return array;
}

答案 1 :(得分:12)

你可以试试这个:


int[][][] data =
{
    new[]
    {
        new[] {1,2,3}
    }, 
    new[]
    {
        new[] {1,2,3}
    }
};

或者没有明确的值:


int[][][] data =
{
    new[]
    {
        Enumerable.Range(1, 100).ToArray()
    }, 
    new[]
    {
        Enumerable.Range(2, 100).ToArray()
    }
};

答案 2 :(得分:7)

没有内置的方法来创建一个数组并在其中创建所有元素,因此它不会接近你想要它的简单程度。它的工作量和实际情况一样多。

您可以创建一个方法来创建数组及其中的所有对象:

public static T[] CreateArray<T>(int cnt, Func<T> itemCreator) {
  T[] result = new T[cnt];
  for (int i = 0; i < result.Length; i++) {
    result[i] = itemCreator();
  }
  return result;
}

然后你可以用它来创建一个三级锯齿状数组:

int[][][] count = CreateArray<int[][]>(10, () => CreateArray<int[]>(10, () => new int[10]));

答案 3 :(得分:1)

三维数组听起来像是创建自己的类的好例子。面向对象可以很漂亮。

答案 4 :(得分:1)

没有比编写2 for循环更“优雅”的方法。这就是为什么它们被称为“锯齿状”,每个子阵列的大小可以变化。

但是这留下了一个问题:为什么不使用[,,]版本?

答案 5 :(得分:1)

int[][][] count = Array.ConvertAll(new bool[10], x =>
                  Array.ConvertAll(new bool[10], y => new int[10]));

答案 6 :(得分:0)

您可以使用具有相同数据表的数据集。这可能就像一个3D对象(xyz =行,列,表)......但无论你做什么,你都会得到一些大的东西;你仍然要占1000件物品。

答案 7 :(得分:0)

Linq

的帮助下
set @profile_rank=0, @current_profile=0; 

select filtered.id_profile, profile.name_profile, count(*) as matching from 
    (
    select * 
    from (select id_profile, id_skill, name_skill as name, ranking_skill, @profile_rank := if(@current_profile = id_profile, @profile_rank + 1, 1) as profile_rank,
    @current_profile := id_profile from 
        (
        select distinct id_profile, skill.id_skill, name_skill, ranking_skill 
        from offer_skill_profile 
        join skill 
        on offer_skill_profile.id_skill = skill.id_skill
        ) sp 
        order by id_profile, ranking_skill desc, name_skill) ranked where id_skill in ('109250886','1224864731') and profile_rank <= 10
    )
    filtered inner join profile on profile.id_profile = filtered.id_profile group by id_profile order by matching desc;

肯定不是很漂亮,而且可能还不够快,但它是单线的。

答案 8 :(得分:0)

为什么不尝试这个?

int[,,] count = new int[10, 10, 10];

您发现这种表示形式有什么问题吗?