C#:将数组拆分为 n 个部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18986129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: Splitting an array into n parts
提问by Blast
I have a list of bytes and I want to split this list into smaller parts.
我有一个字节列表,我想将此列表拆分为更小的部分。
var array = new List<byte> {10, 20, 30, 40, 50, 60};
This list has 6 cells. For example, I want to split it into 3 parts containing each 2 bytes.
此列表有 6 个单元格。例如,我想将它分成 3 部分,每部分包含 2 个字节。
I have tried to write some for loops and used 2D arrays to achieve my purpose but I don't know it is a correct approach.
我尝试编写一些 for 循环并使用 2D 数组来实现我的目的,但我不知道这是一种正确的方法。
byte[,] array2D = new byte[window, lst.Count / window];
var current = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
array2D[i, j] = lst[current++];
}
}
采纳答案by ZenLulz
A nice way would be to create a generic/extension method to split any array. This is mine:
一个不错的方法是创建一个通用/扩展方法来拆分任何数组。这是我的:
/// <summary>
/// Splits an array into several smaller arrays.
/// </summary>
/// <typeparam name="T">The type of the array.</typeparam>
/// <param name="array">The array to split.</param>
/// <param name="size">The size of the smaller arrays.</param>
/// <returns>An array containing smaller arrays.</returns>
public static IEnumerable<IEnumerable<T>> Split<T>(this T[] array, int size)
{
for (var i = 0; i < (float)array.Length / size; i++)
{
yield return array.Skip(i * size).Take(size);
}
}
Moreover, this solution is deferred. Then, simply call split(size)
on your array.
此外,该解决方案被推迟。然后,只需调用split(size)
您的数组。
var array = new byte[] {10, 20, 30, 40, 50};
var splitArray = array.Split(2);
As requested, here is a generic/extension method to get a square 2D arrays from an array:
根据要求,这是从数组中获取方形二维数组的通用/扩展方法:
public static T[,] ToSquare2D<T>(this T[] array, int size)
{
var buffer = new T[(int)Math.Ceiling((double)array.Length/size), size];
for (var i = 0; i < (float)array.Length / size; i++)
{
for (var j = 0; j < size; j++)
{
buffer[i, j] = array[i + j];
}
}
return buffer;
}
Have fun :)
玩得开心 :)
回答by Omri Btian
回答by Hossain Muctadir
You might want to give this a try.
您可能想尝试一下。
var bytes = new List<byte>(10000);
int size = 100;
var lists = new List<List<byte>>(size);
for (int i = 0; i < bytes.Count; i += size)
{
var list = new List<byte>();
list.AddRange(bytes.GetRange(i, size));
lists.Add(list);
}
回答by Francesco
Here my naif solution:
这是我的天真解决方案:
public static string[] SplitArrey(string[] ArrInput, int n_column)
{
string[] OutPut = new string[n_column];
int NItem = ArrInput.Length; // Numero elementi
int ItemsForColum = NItem / n_column; // Elementi per arrey
int _total = ItemsForColum * n_column; // Emelemti totali divisi
int MissElement = NItem - _total; // Elementi mancanti
int[] _Arr = new int[n_column];
for (int i = 0; i < n_column; i++)
{
int AddOne = (i < MissElement) ? 1 : 0;
_Arr[i] = ItemsForColum + AddOne;
}
int offset = 0;
for (int Row = 0; Row < n_column; Row++)
{
for (int i = 0; i < _Arr[Row]; i++)
{
OutPut[Row] += ArrInput[i + offset] + " "; // <- Here to change how the strings are linked
}
offset += _Arr[Row];
}
return OutPut;
}
回答by misha130
This to have list of lists
这有列表列表
array.Select((s,i) => array.Skip(i * 2).Take(2)).Where(a => a.Any())
Or this to have list of items
或者这个有项目列表
array.SelectMany((s,i) => array.Skip(i * 2).Take(2)).Where(a => a.Any())