如何在C#中将数组转换为某种大小的数组列表

Vik*_*oss 0 c# arrays list

如何在C#中将数组转换为某种大小的数组列表?

例如:

 byte[] incoming = {1,2,3,4};
 List<byte[]> chunks = new List<byte[]>; 
Run Code Online (Sandbox Code Playgroud)

我想要的是这样的东西,得到一些大小,下面我用2.

 chunks[0] = {1,2};
 chunks[1] = {3,4};
Run Code Online (Sandbox Code Playgroud)

提前致谢!

Rob*_*vey 7

这个辅助方法应该使事情变得更容易:

public static byte[] Partial(byte[] source, int start, int length)
{
    byte[] b = new byte[length];
    Array.Copy(source, start, b, 0, length);
    return b;
}    
Run Code Online (Sandbox Code Playgroud)

从那里,你可以做一些事情:

for (int index = 0; index < incoming.Length; index += 2)
{
    List.Add(Partial(incoming, index, 2));
}
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用类似后一部分的内容添加到返回`new byte [] {incoming [index],incoming [index + 1]}`的列表中.此外,如果您需要一个数组,那么您只需使用`listvar.ToArray()`来获取最后的数组. (2认同)