English 中文(简体)
附上一份C#目录。
原标题:Concatenating a C# List of byte[]

我正在制造一些需要联合起来的星座阵列,以建立一个大型的旁观阵列,即更不用说只使用,但在这里别无选择。

在我创建名单时,我把每一个名单都列入名单,因此,我只必须做一句话,而我的问题是,这样做的最佳方式是什么?

当我有一个人数不详的星号清单时,我想把他们集中起来。

感谢。

最佳回答
listOfByteArrs.SelectMany(byteArr=>byteArr).ToArray()

The above code will concatenate a sequence of sequences of bytes into one sequence - and store the result in an array.

虽然可以读懂,但这并非最有效率的——它没有利用你已读到<>的事实。 由此形成的星阵列的长度,从而可以避免动态扩展的<代码>。 此外,<代码>SelectMany在器具方面实施;这意味着接口电话的批量+位置相当缓慢。 然而,对于数据集的较小规模来说,这不太可能发生。

www.un.org/Depts/DGACM/index_french.htm

var output = new byte[listOfByteArrs.Sum(arr=>arr.Length)];
int writeIdx=0;
foreach(var byteArr in listOfByteArrs) {
    byteArr.CopyTo(output, writeIdx);
    writeIdx += byteArr.Length;
}

or as Martinho suggests:

var output = new byte[listOfByteArrs.Sum(arr => arr.Length)];
using(var stream = new MemoryStream(output))
    foreach (var bytes in listOfByteArrs)
        stream.Write(bytes, 0, bytes.Length);

一些时间:

var listOfByteArrs = Enumerable.Range(1,1000)
    .Select(i=>Enumerable.Range(0,i).Select(x=>(byte)x).ToArray()).ToList();

采用短期方法,将5 500只矿石压缩到15米,使用快速方法,在我的机器上使用0.5米,并且注意到,对于许多申请,都超过快;-

最后,请将<代码>Array.CopyTo>改为static、Array.Copy、低级<代码>Buffer.BlockCopy或MemoryStream, 附有预先定位的后身缓冲,这些性能在我的测试(x64 .NET 4.0)上基本相同。

问题回答

Here s a solution based on Andrew Bezzub s and fejesjoco s answers, pre-allocating all the memory needed up front. This yields Θ(N) memory usage and Θ(N) time (N being the total number of bytes).

byte[] result = new byte[list.Sum(a => a.Length)];
using(var stream = new MemoryStream(result))
{
    foreach (byte[] bytes in list)
    {
        stream.Write(bytes, 0, bytes.Length);
    }
}
return result;

他们都写给记忆犹新,而不是一个清单。 然后称为“记忆”。 ToArray(). 或者,如果你有这份清单,首先总结一下所有按字母顺序排列的阵列长度,建立一个总长度的新星阵列,并在最后几个阵列中复制每一阵列。

Use Linq:

    List<byte[]> list = new List<byte[]>();
    list.Add(new byte[] { 1, 2, 3, 4 });
    list.Add(new byte[] { 1, 2, 3, 4 });
    list.Add(new byte[] { 1, 2, 3, 4 });

    IEnumerable<byte> result = Enumerable.Empty<byte>();

    foreach (byte[] bytes in list)
    {
        result = result.Concat(bytes);
    }

    byte[] newArray = result.ToArray();

或许更快的解决办法是(而不是宣布表面上):

IEnumerable<byte> bytesEnumerable = GetBytesFromList(list);

byte[] newArray = bytesEnumerable.ToArray();

private static IEnumerable<T> GetBytesFromList<T>(IEnumerable<IEnumerable<T>> list)
{
    foreach (IEnumerable<T> elements in list)
    {
        foreach (T element in elements)
        {
            yield return element;
        }
    }
}

上文似乎只想一劳永逸地打下每一阵列。

相反,你可以把每一批次的阵列储存成<代码>List<byte[]>直接添加到<代码>List<byte>,使用/AddRange 每种方法。

hmm How about ?





相关问题
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. ...

热门标签