C# 如何将二维数组中的一行值复制到一维数组中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/797354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:33:30  来源:igfitidea点击:

How to copy a row of values from a 2D array into a 1D array?

c#.netarraysmultidimensional-array

提问by stevehipwell

We have the following object

我们有以下对象

int [,] oGridCells;

which is only used with a fixed first index

仅与固定的第一个索引一起使用

int iIndex = 5;
for (int iLoop = 0; iLoop < iUpperBound; iLoop++)
{
  //Get the value from the 2D array
  iValue = oGridCells[iIndex, iLoop];

  //Do something with iValue
}

Is there a way in .NET to convert the values at a fixed first index into a single dimension array (other than by looping the values)?

.NET 中有没有办法将固定第一个索引处的值转换为单维数组(除了循环值)?

I doubt it would speed up the code (and it may well make it slower) if the array is only being looped once. But if the array was being heavily manipulated then a single dimension array would be more efficient than a multi dimension array.

我怀疑如果数组只循环一次,它会加速代码(并且很可能使它变慢)。但是如果数组被大量操作,那么一维数组将比多维数组更有效。

My main reason for asking the question is to see if it can be done and how, rather than using it for production code.

我提出这个问题的主要原因是看看它是否可以完成以及如何完成,而不是将它用于生产代码。

采纳答案by BlueMonkMN

The following code demonstrates copying 16 bytes (4 ints) from a 2-D array to a 1-D array.

以下代码演示了如何将 16 个字节(4 个整数)从二维数组复制到一维数组。

int[,] oGridCells = {{1, 2}, {3, 4}};
int[] oResult = new int[4];
System.Buffer.BlockCopy(oGridCells, 0, oResult, 0, 16);

You can also selectively copy just 1 row from the array by providing the correct byte offsets. This example copies the middle row of a 3-row 2-D array.

您还可以通过提供正确的字节偏移量从数组中选择性地仅复制 1 行。此示例复制 3 行二维数组的中间行。

int[,] oGridCells = {{1, 2}, {3, 4}, {5, 6}};
int[] oResult = new int[2];
System.Buffer.BlockCopy(oGridCells, 8, oResult, 0, 8);

回答by Daren Thomas

I'd be suprised if it were possible: I bet oGridCells[iIndex, iLoop]is just a sort of shorthand (internally, in MSIL) for oGridCells[iIndex * iLoop], and that multidimensional arrays are syntactic sugar for this.

如果可能的话,我会感到惊讶:我敢打赌,oGridCells[iIndex, iLoop]它只是 的一种简写(在内部,在 MSIL 中)oGridCells[iIndex * iLoop],而多维数组就是它的语法糖。

To answer your question: No. You will have to loop the values.

回答你的问题:不。你必须循环这些值。

回答by Matthew Flaschen

Edit:

编辑:

I realized there is a way! Granted, it's probably not worth it. Use unsafe code. Full example, showing both ways, with unsafe below:

我意识到有办法!当然,这可能不值得。使用不安全的代码。完整示例,显示两种方式,以下不安全:

public class MultiSingleUnsafe
{
    public static unsafe void Main(String[] a)
    {
    int rowCount = 6;
    int iUpperBound = 10;
    int [,] oGridCells = new int[rowCount, iUpperBound];

    int iIndex = rowCount - 2; // Pick a row.

    for(int i = 0; i < iUpperBound; i++)
    {
        oGridCells[iIndex, i] = i;
    }

    for (int iLoop = 0; iLoop < iUpperBound; iLoop++)
    {
        //Get the value from the 2D array
        int iValue = oGridCells[iIndex, iLoop];
        Console.WriteLine("Multi-dim array access iValue: " + iValue);
        //Do something with iValue
    }

    fixed(int *lastRow = &(oGridCells[iIndex,0]))
    {   
        for (int iLoop = 0; iLoop < iUpperBound; iLoop++)
        {
        int iValue = lastRow[iLoop];
        Console.WriteLine("Pointer access iValue: " + iValue);
        }
    }
    }
}

There is no way I know to cast a multiple-dimensional array into a single-dimensional one in C#. Of course, you could create a new single-dimensional array and copy into it. But I don't think this will get a performance benefit even if you loop over the values multiple times. As Daren said, internally it's all pointer arithmetic anyway. If you want to be certain, profile it.

我不知道在 C# 中将多维数组转换为一维数组。当然,您可以创建一个新的一维数组并将其复制到其中。但是我认为即使您多次循环这些值,这也不会获得性能优势。正如达伦所说,无论如何,内部都是指针算术。如果您想确定,请对其进行概要分析。

回答by Groo

You cannot get a reference to each array. You can, however, use a jagged array.

您无法获得对每个数组的引用。但是,您可以使用锯齿状数组

回答by AnnaR

"But if the array was being heavily manipulated then a single dimension array would be more efficient than a multi dimension array."

“但如果数组被大量操作,那么一维数组将比多维数组更有效。”

I did some profiling of exactly this last summer and was surprised to see no significant difference in performance between a 2D and 1D array.

去年夏天我做了一些分析,并惊讶地发现 2D 和 1D 阵列之间的性能没有显着差异。

I didn't test the performance of a jagged array.

我没有测试锯齿状数组的性能。

回答by Willy David Jr

You can try this:

你可以试试这个:

 int[,] twoD = new int[2,2];
 twoD[0, 0] = 1;
 twoD[0, 1] = 2;
 twoD[1, 0] = 3;
 twoD[1, 1] = 4;

 int[] result = twoD.Cast<int>().Select(c => c).ToArray();

The result will be an integer array with data:

结果将是一个包含数据的整数数组:

1, 2, 3, 4