Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.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语言中从特定多维数组中提取列#_C#_Multidimensional Array - Fatal编程技术网

C# 在C语言中从特定多维数组中提取列#

C# 在C语言中从特定多维数组中提取列#,c#,multidimensional-array,C#,Multidimensional Array,尝试从数组中提取行时遇到问题。这是我的代码: using System; namespace C_sharp { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!. This is the one !"); dynamic[] res = new Object[3];

尝试从数组中提取行时遇到问题。这是我的代码:

using System;

namespace C_sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!. This is the one !");

            dynamic[] res = new Object[3];

            res[0] = new int[2,2]{{3,2},{16,9}};
            res[1] = new int[2,2]{{4,7},{6,29}};
            res[2] = new int[2,2]{{30,29},{5,49}};

            //dynamic resultat = res[0][0];

        }
    }
}
如您所见,在我的程序结束时,我的局部变量
res
的内容如下所示:

[0] [dynamic]:{int[2, 2]}
[0, 0] [int]:3
[0, 1] [int]:2
[1, 0] [int]:16
[1, 1] [int]:9
[1] [dynamic]:{int[2, 2]}
[0, 0] [int]:4
[0, 1] [int]:7
[1, 0] [int]:6
[1, 1] [int]:29
[2] [dynamic]:{int[2, 2]}
[0, 0] [int]:30
[0, 1] [int]:29
[1, 0] [int]:5
[1, 1] [int]:49
现在,我只想把
res[0]
的第一行放在变量
resultat
中,以获得1D数组。因此,在流程结束时,
resultat
应等于:

[0] [int]:3
[1] [int]:2

我已经试过了
res[0][0、]
res[0][0:][/code>。他们都没有工作。有什么帮助吗?

您可以使用
.Cast()
展平二维数组,然后使用
获取第一行。获取(2)

一般情况下是这样的

const int intSize = 4; // Size of integer type in bytes

int[,] matrix = res[0];

int rowSize = matrix.GetLength(1); // Get size of second 2d-array dimension

int rowIndex = 0; // Index of a row you want to extract

int[] resultat = new int[rowSize];
Buffer.BlockCopy(res[0], // Copy source
            rowSize * intSize * rowIndex, // Source offset in bytes
            resultat, // Copy destination
            0,  // Destination offset
            intSize * rowSize); // The number of bytes to copy 

为什么使用
dynamic[]
而不是
int[][,]
?使用
dynamic[]
,我可以通过执行例如
res[0][I,j]
来访问矩阵的任何值。类型
Object
(并且
int
也是
对象
)是不可能的。您是否尝试过
selectMany(predicate())
。。。?如果操作正确,可以使用
int[][,]
执行此操作:
int[][,]res=new int[3][,]@CamiloTerevinto,你说得对。但在我的项目中,我使用了其他类型的结构,我刚刚粘贴了上面的代码片段来给出一个示例。现在,我正在处理大量的数据,我想要一个接一个的colunm。有什么想法吗?谢谢…嗨,非常感谢你的回答!!:D我正在使用您的最后一个代码:
Buffer.BlockCopy
。问题是,我正在使用列出的参数的不同值(
arraySize
intSize
rowIndex
)进行测试,并且出现了
异常。因此,我不确定这些参数到底意味着什么。为了添加更多细节,在我的示例中,
res[0]
只向
resultat
返回2个元素,但在我的项目中,我不知道应该向变量
resultat
返回多少元素@BoubacarTraoréintSize表示整数的大小-4字节,如果使用另一种类型,则应该正确更改它。@BoubacarTraoré使用.GetLength(1)来获取它。我已经编辑了答案,希望现在更清楚。你是天才@Bagdan Gilevich,它最终解决了我的问题!!非常感谢;-)
const int rowSize = 2;
const int intSize = 4;

int[] resultat = new int[rowSize];
Buffer.BlockCopy(res[0], 0, resultat, 0, intSize * rowSize);
const int intSize = 4; // Size of integer type in bytes

int[,] matrix = res[0];

int rowSize = matrix.GetLength(1); // Get size of second 2d-array dimension

int rowIndex = 0; // Index of a row you want to extract

int[] resultat = new int[rowSize];
Buffer.BlockCopy(res[0], // Copy source
            rowSize * intSize * rowIndex, // Source offset in bytes
            resultat, // Copy destination
            0,  // Destination offset
            intSize * rowSize); // The number of bytes to copy