Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数组中的多维数组_C#_Arrays_Multidimensional Array - Fatal编程技术网

C# 数组中的多维数组

C# 数组中的多维数组,c#,arrays,multidimensional-array,C#,Arrays,Multidimensional Array,我试图找出如何用可能的普通数组制作2D数组。 下面是一个例子,我是如何想象的: int[,] original = new int[,] {{1,1,1,1}, {1,0,0,1}, {1,0,0,1}, {1,1,1,1}}; int[] part = {1,1,1,1}; int[,] copy = new int[,] {part}, {1,0,0,1}, {1,0,0,1}, {1,1,1,1}}; 这在本机2d阵列中通常不容易做到,在初始化期间也不可能做到 但可以使用以下扩展方法修

我试图找出如何用可能的普通数组制作2D数组。 下面是一个例子,我是如何想象的:

int[,] original = new int[,] {{1,1,1,1}, {1,0,0,1}, {1,0,0,1}, {1,1,1,1}};

int[] part = {1,1,1,1};
int[,] copy = new int[,] {part}, {1,0,0,1}, {1,0,0,1}, {1,1,1,1}};

这在本机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);
        }
    }
}

到目前为止你做了什么?你能告诉我们你的尝试吗?你卡在哪里了?有什么错误吗?如果是,请将其添加到问题中
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);
        }
    }
}