一个不错的方法是创建一个通用/扩展方法来拆分任何数组。这是我的:
/// <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;
}
玩得开心!