したがって、すべてのアレイが同じように作成されているわけではないことがわかります。多次元配列は、ゼロ以外の下限を持つことができます。たとえば、ExcelPIAのRange.Valueプロパティを参照してくださいobject[,] rectData = myRange.Value;
これらのデータをギザギザの配列に変換する必要があります。以下の私の最初の試みは、複雑さの匂いがします。それを最適化するための提案はありますか?下限がゼロでない可能性がある一般的なケースを処理する必要があります。
私はこのexメソッドを持っています:
public static T[][] AsJagged<T>( this T[,] rect )
{
int row1 = rect.GetLowerBound(0);
int rowN = rect.GetUpperBound(0);
int col1 = rect.GetLowerBound(1);
int colN = rect.GetUpperBound(1);
int height = rowN - row1 + 1;
int width = colN - col1 + 1;
T[][] jagged = new T[height][];
int k = 0;
int l;
for ( int i = row1; i < row1 + height; i++ )
{
l = 0;
T[] temp = new T[width];
for ( int j = col1; j < col1 + width; j++ )
temp[l++] = rect[i, j];
jagged[k++] = temp;
}
return jagged;
}
このように使用されます:
public void Foo()
{
int[,] iRect1 = { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 } };
int[][] iJagged1 = iRect1.AsJagged();
int[] lengths = { 3, 5 };
int[] lowerBounds = { 7, 8 };
int[,] iRect2 = (int[,])Array.CreateInstance(typeof(int), lengths, lowerBounds);
int[][] iJagged2 = iRect2.AsJagged();
}
Buffer.BlockCopy ()が機能するのか、それとも高速になるのか知りたいですか?
編集:AsJaggedは参照型を処理する必要があります。
編集:AsJagged()でバグが見つかりました。追加int l
; col1 + width
内側のループに追加されます。