Это обычно нелегко сделать с собственными 2d-массивами и невозможно сделать во время инициализации.
Но вы можете изменить существующий 2d-массив следующими методами расширения.
static class Program
{
static void Main(string[] args)
{
int[,] original = new int[,] {
{ 1, 1, 1, 1 },
{ 1, 0, 0, 1 },
{ 1, 0, 0, 1 },
{ 1, 1, 1, 1 } };
int[] row_0 = original.GetRow(0);
// {1,1,1,1}
int[] part = { 1, 2, 3, 4 };
original.SetRow(0, part);
// {{1,2,3,4},{1,0,0,1},...}
}
public static T[] GetRow<T>(this T[,] matrix, int row)
{
int rows = matrix.GetLength(0);
int columns = matrix.GetLength(1);
T[] result = new T[columns];
if (row<=rows)
{
int size = Buffer.ByteLength(matrix)/rows;
Buffer.BlockCopy(matrix, row*size, result, 0, size);
}
return result;
}
public static void SetRow<T>(this T[,] matrix, int row, params T[] elements)
{
int rows = matrix.GetLength(0);
int columns = matrix.GetLength(1);
if (row<rows && elements.Length == columns)
{
int size = Buffer.ByteLength(elements);
Buffer.BlockCopy(elements, 0, matrix, row*size, size);
}
}
}
Конечно, та же функциональность тривиальна для зубчатых массивов. Вместо int[,]
используйте int[][]
.
static class Program
{
static void Main(string[] args)
{
int[][] original = new int[][] {
new int[] { 1, 1, 1, 1 },
new int[] { 1, 0, 0, 1 },
new int[] { 1, 0, 0, 1 },
new int[] { 1, 1, 1, 1 }
};
int[] row_0 = original[0];
// { 1, 1, 1, 1}
int[] parts = new[] { 1, 2, 3, 4 }; // implicit int[] type
original[0] = parts;
// { {1, 2, 3, 4}, {1, 0, 0, 1},..}
int[,] matrix = original.JaggedToMatrix();
}
public static T[,] JaggedToMatrix<T>(this T[][] jagged)
{
int rows = jagged.Length;
int columns = jagged.Length>0 ? jagged[0].Length : 0;
T[,] result = new T[rows, columns];
for (int i = 0; i < rows; i++)
{
result.SetRow(i, jagged[i]);
}
return result;
}
public static T[][] MatrixToJagged<T>(this T[,] matrix)
{
int rows = matrix.GetLength(0);
int columns = matrix.GetLength(1);
T[][] result = new T[rows][];
for (int i = 0; i < rows; i++)
{
result[i] = matrix.GetRow(i);
}
return result;
}
public static T[] GetRow<T>(this T[,] matrix, int row)
{
int rows = matrix.GetLength(0);
int columns = matrix.GetLength(1);
T[] result = new T[columns];
if (row<=rows)
{
int size = Buffer.ByteLength(matrix)/rows;
Buffer.BlockCopy(matrix, row*size, result, 0, size);
}
return result;
}
public static void SetRow<T>(this T[,] matrix, int row, params T[] elements)
{
int rows = matrix.GetLength(0);
int columns = matrix.GetLength(1);
if (row<rows && elements.Length == columns)
{
int size = Buffer.ByteLength(elements);
Buffer.BlockCopy(elements, 0, matrix, row*size, size);
}
}
}