36

我有一个字节列表,我想把这个列表分成更小的部分。

var array = new List<byte> {10, 20, 30, 40, 50, 60};

此列表有 6 个单元格。例如,我想将它分成 3 个部分,每个部分包含 2 个字节。

我尝试编写一些 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

5 回答 5

76

一个不错的方法是创建一个通用/扩展方法来拆分任何数组。这是我的:

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

此外,该解决方案被推迟。然后,只需调用Split(size)您的阵列。

var array = new byte[] {10, 20, 30, 40, 50, 60};
var splitArray = array.Split(2);

根据要求,这是从数组中获取方形二维数组的通用/扩展方法:

/// <summary>
/// Splits a given array into a two dimensional arrays of a given size.
/// The given size must be a divisor of the initial array, otherwise the returned value is <c>null</c>,
/// because not all the values will fit into the resulting array.
/// </summary>
/// <param name="array">The array to split.</param>
/// <param name="size">The size to split the array into. The size must be a divisor of the length of the array.</param>
/// <returns>
/// A two dimensional array if the size is a divisor of the length of the initial array, otherwise <c>null</c>.
/// </returns>
public static T[,]? ToSquare2D<T>(this T[] array, int size)
{
    if (array.Length % size != 0) return null;

    var firstDimensionLength = array.Length / size;
    var buffer = new T[firstDimensionLength, size];

    for (var i = 0; i < firstDimensionLength; i++)
    {
        for (var j = 0; j < size; j++)
        {
            buffer[i, j] = array[i * size + j];
        }
    }

    return buffer;
}

玩得开心!

于 2013-09-24T16:53:54.483 回答
14

使用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();
}

只需使用它

var sublists = SplitToSublists(lst);
于 2013-09-24T15:52:19.350 回答
7

这有列表的列表

array.Select((s,i) => array.Skip(i * 2).Take(2)).Where(a => a.Any())

或者这个有项目列表

array.SelectMany((s,i) => array.Skip(i * 2).Take(2)).Where(a => a.Any())
于 2018-09-16T12:29:16.797 回答
-1

您可能想尝试一下。

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);
}
于 2013-09-24T15:55:03.197 回答
-1

如果您有一个要划分的数组,但通过这个简单的解决方案,您可以将缺少的元素平等地共享到各个“块”中:

    public static List<int[]> SplitArrey(int[] arrInput, int nColumn) {

    List<int[]> result = new List<int[]>(nColumn);

    int itemsForColum = arrInput.Length / nColumn;  
    int countSpareElement = arrInput.Length - (itemsForColum * nColumn);    

    // Add and extra space for the spare element
    int[] newColumLenght = new int[nColumn];
    for (int i = 0; i < nColumn; i++)
    {
        int addOne = (i < countSpareElement) ? 1 : 0;
        newColumLenght[i] = itemsForColum + addOne;
        result.Add(new int[itemsForColum + addOne]);
    }

    // Copy the values
    int offset = 0;
    for (int i = 0; i < nColumn; i++)
    {
        int count_items_to_copy = newColumLenght[i];
        Array.Copy(arrInput, offset, result[i], 0, count_items_to_copy);
        offset += newColumLenght[i];
    }
    return result;
}

然后:

int[] arrInput = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
var result = SplitArrey(arrInput, 5);
foreach (var item in result) {
 Console.WriteLine("   {0}", String.Join(" ", item));
}

结果是:

1 2 3 
4 5 6 
7 8 
9 10 
11 12
于 2016-02-28T22:18:47.177 回答