首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将单行从多维数组复制到新的一维数组中

将单行从多维数组复制到新的一维数组中
EN

Stack Overflow用户
提问于 2016-01-25 06:18:51
回答 3查看 3.1K关注 0票数 3

我希望将多维数组中的特定行复制到一个新的一维数组中,该数组可以在代码中的其他地方使用。

输入:

多维数组3,3

代码语言:javascript
复制
33 300 500,
56 354 516,
65 654 489,

所需产出:

一维阵列(第二线)

代码语言:javascript
复制
56 354 516
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-01-25 06:28:50

代码语言:javascript
复制
var source = new int[3, 3]
{
    { 33, 300, 500 },
    { 56, 354, 516 },
    { 65, 654, 489 }
};
// initialize destination array with expected length
var dest = new int[source.GetLength(1)];

// define row number
var rowNumber = 1;

// copy elemements to destination array
for (int i = 0; i < source.GetLength(1); i++)
{
    dest[i] = (int) source.GetValue(rowNumber, i);
}
票数 2
EN

Stack Overflow用户

发布于 2016-01-25 06:36:05

在这种情况下,Buffer.BlockCopy可能会派上用场:

代码语言:javascript
复制
int[,] original = new int[3, 3]
{
    { 33, 300, 500 },
    { 56, 354, 516 },
    { 65, 654, 489 }
};

int[] target = new int[3];
int rowIndex = 1; //get the row you want to extract your data from (start from 0)
int columnNo = original.GetLength(1); //get the number of column
Buffer.BlockCopy(original, rowIndex * columnNo * sizeof(int), target, 0, columnNo * sizeof(int));

你将进入你的target

代码语言:javascript
复制
56, 354, 516
票数 4
EN

Stack Overflow用户

发布于 2016-01-25 06:32:58

应该是这样的:

代码语言:javascript
复制
        int[][] arrayComplex = {new[] {33, 300, 500},new []{56, 354, 516}, new []{65, 654, 489}};
        int[] arraySingle = new int[3];
        for (int i = 0; i < arrayComplex[1].Length; i++)
        {
            arraySingle[i] = arrayComplex[1][i];
        }

        foreach (var i in arraySingle)
        {
            Console.Write(i + "  ");
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34986250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档