【发布时间】:2021-02-04 18:27:24
【问题描述】:
扩展名:
public static T[,] SubArray<T>(this T[,] values, int row_min, int row_max, int col_min, int col_max)
{
int num_rows = row_max - row_min + 1;
int num_cols = col_max - col_min + 1;
T[,] result = new T[num_rows, num_cols];
int total_cols = values.GetUpperBound(1) + 1;
int from_index = row_min * total_cols + col_min;
int to_index = 0;
for (int row = 0; row <= num_rows - 1; row++)
{
Array.Copy(values, from_index, result, to_index, num_cols);
from_index += total_cols;
to_index += num_cols;
}
return result;
}
适用于GetLowerBound(0) 和GetLowerBound(1) 等于0 的二维数组。例如如果
int[,] arr1 = new int[5, 4];
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 4; ++j)
{
arr1[i, j] = i + j;
}
}
var arr1sub = arr1.SubArray(2, 3, 1, 3);
那么arr1sub是2行3列的二维数组(索引都从0开始)
3 4 5
5 6 7
现在,如果我看一下初始数组作为索引不从零开始的情况:
int[,] arr2 = (int[,])Array.CreateInstance(typeof(int), new int[] { 5, 4 }, new int[] { 3, 1 });
for (int i = arr2.GetLowerBound(0); i <= arr2.GetUpperBound(0); ++i)
{
for (int j = arr2.GetLowerBound(1); j <= arr2.GetUpperBound(1); ++j)
{
arr2[i, j] = i - arr2.GetLowerBound(0) + j - arr2.GetLowerBound(1);
}
}
var arr2sub = arr2.SubArray(5, 6, 2, 4);
前面代码的最后一行sn -p会在该行的SubArray扩展函数中触发异常
Array.Copy(values, from_index, result, to_index, num_cols);
对于row 等于零。
我了解二维数组 arr1(具有从零开始的索引)在内存中的布局,但不了解二维数组 arr2(具有非从零开始的索引)在内存中的布局方式,因此我使用Array.Copy 在这种情况下肯定是错误的,但我不明白为什么。
【问题讨论】:
标签: c# arrays multidimensional-array