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

前端 未结 6 2141
醉话见心
醉话见心 2020-12-05 08:17

We have the following object

int [,] oGridCells;

which is only used with a fixed first index

int iIndex = 5;
for (int iLoop         


        
相关标签:
6条回答
  • 2020-12-05 08:35

    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.

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

    0 讨论(0)
  • 2020-12-05 08:39

    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
    
    0 讨论(0)
  • 2020-12-05 08:40

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

    0 讨论(0)
  • 2020-12-05 08:47

    "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.

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

    0 讨论(0)
  • 2020-12-05 08:51

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

    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.

    int[,] oGridCells = {{1, 2}, {3, 4}, {5, 6}};
    int[] oResult = new int[2];
    System.Buffer.BlockCopy(oGridCells, 8, oResult, 0, 8);
    
    0 讨论(0)
  • 2020-12-05 08:51

    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.

    0 讨论(0)
提交回复
热议问题