【问题标题】:Multidimensional array from arrays来自数组的多维数组
【发布时间】:2020-03-15 16:14:35
【问题描述】:

我正在尝试找出如何用可能的普通数组制作二维数组。 这是一个我怎么想象的例子:

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}};

【问题讨论】:

  • 到目前为止您尝试了什么?你能告诉我们你的尝试吗?你卡在哪里了?有什么错误吗?如果是,请在问题中添加它

标签: c# arrays multidimensional-array


【解决方案1】:

这对于原生二维数组来说通常不容易做到,并且在初始化期间也不可能做到。

但您可以使用以下扩展方法修改现有的二维数组。

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);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 2018-02-11
    • 2014-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多