C#:将数组拆分为n个部分

时间:2022-09-12 10:55:22

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++];
                }
            }

4 个解决方案

#1


37  

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:

根据要求,这是一个从数组中获取方形2D数组的通用/扩展方法:

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 :)

玩的开心 :)

#2


10  

using Linq

使用Linq

public List<List<byte>> SplitToSublists(List<byte> source)
{
    return source
             .Select((x, i) => new { Index = i, Value = x })
             .GroupBy(x => x.Index / 100)
             .Select(x => x.Select(v => v.Value).ToList())
             .ToList();
}

Simply use it

只需使用它

var sublists = SplitToSublists(lst);

#3


0  

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);
}

#4


0  

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;
    }

#1


37  

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:

根据要求,这是一个从数组中获取方形2D数组的通用/扩展方法:

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 :)

玩的开心 :)

#2


10  

using Linq

使用Linq

public List<List<byte>> SplitToSublists(List<byte> source)
{
    return source
             .Select((x, i) => new { Index = i, Value = x })
             .GroupBy(x => x.Index / 100)
             .Select(x => x.Select(v => v.Value).ToList())
             .ToList();
}

Simply use it

只需使用它

var sublists = SplitToSublists(lst);

#3


0  

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);
}

#4


0  

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;
    }