English 中文(简体)
采用准则Q采用双重[][][]
原标题:Initialize double[][][] as double[1][2][3] using LINQ
  • 时间:2012-04-10 12:06:40
  •  标签:
  • c#
  • linq

我们能够利用林克,将一系列的双重[][][][]定为双重[3.1] [3](这并不是正确的合成物)。

使用一种方式

double[][][] myarr = new double[1][][];
for(int i=0; i<1; i++)
{
    myarr[i] = new double[2][];
    for(int j=0; j<2; j++)
    {
         myarr[i][j] = new double[3];
    }
}

但是,我希望有一部更清洁的法典。 我尝试了选择,但只填补了第一级。 如何做到这一点。 成就

并且,这并不是家庭工作!

最佳回答
double[][][] threeDimensionArray = 
  Enumerable.Range(0, 1)
            .Select(h1 => Enumerable.Range(0, 2)
                                    .Select(h2 => new double[3])
                                    .ToArray())
            .ToArray();

但是,这要求有多个<代码>ToArray()的电话,这些电话的复印件(见下面的英文版),因此,对于大量物品而言,这种“合法”解决办法并非免费。 BTW,我倾向于for loop Solutions。

IL Spy

// System.Linq.Enumerable
public static TSource[] ToArray<TSource>(this IEnumerable<TSource> source)
{
    if (source == null)
    {
        throw Error.ArgumentNull("source");
    }

     // sll: see implementation of Buffer.ToArray() below
    return new Buffer<TSource>(source).ToArray();
}

// System.Linq.Buffer<TElement>
internal TElement[] ToArray()
{
    if (this.count == 0)
    {
        return new TElement[0];
    }
    if (this.items.Length == this.count)
    {
        return this.items;
    }
    TElement[] array = new TElement[this.count];
    Array.Copy(this.items, 0, array, 0, this.count);
    return array;
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签