Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to extract a one-dimensional array from a rectangular array in C#?

Say I have a rectangular string array - not a jagged array

string[,] strings = new string[8, 3];

What's the best way to extract a one-dimensional array from this (either a single row or a single column)? I can do this with a for loop, of course, but I'm hoping .NET has a more elegant way built in.

Bonus points for converting the extracted string array to an object array.

like image 345
MusiGenesis Avatar asked Oct 24 '08 05:10

MusiGenesis


People also ask

How do you convert a two-dimensional array to a one-dimensional array?

In this C# Program, we are reading the elements of the 2-Dimensional matrix. Using for loop assign the value of 'a[i,j]' variable to b[] array variable. Increment the value of base index 'k' variable. Print the value of one dimensional array.

How do you make an array one-dimensional?

Rules for Declaring One Dimensional ArrayThe declaration must have a data type(int, float, char, double, etc.), variable name, and subscript. The subscript represents the size of the array. If the size is declared as 10, programmers can store 10 elements. An array index always starts from 0.

Is 1D array faster than 2D?

Unless you are talking about static arrays, 1D is faster. Clearly the 2D case loses the cache locality and uses more memory. It also introduces an extra indirection (and thus an extra pointer to follow) but the first array has the overhead of calculating the indices so these even out more or less.

What is one-dimensional array in C with example?

Rules for Declaring One Dimensional Array in C In array, indexing starts from 0 and ends at size-1. For example, if we have arr[10] of size 10, then indexing of elements ranges from 0 to 9. We must include data-type and variable name while declaring one-dimensional arrays in C.


1 Answers

You can cast a string array to an object array trivially - going the other way doesn't work. The actual extraction has to use a for loop though, as far as I can see: Array.Copy requires the source and target ranks to be the same, and Buffer.BlockCopy only works for value type arrays. It does seem odd though...

You can use LINQ to extra a row or column in a single statement, although it will be inefficient (as it'll build up a list internally, then have to convert it to an array - if you do it yourself you can preallocate the array to the right size and copy directly).

Copying a row (rowNum is the row to be copied):

object[] row = Enumerable.Range(0, rowLength)
                         .Select(colNum => (object) stringArray[rowNum, colNum])
                         .ToArray();

Copying a column (colNum is the column to be copied):

object[] column = Enumerable.Range(0, columnLength)
                            .Select(rowNum => (object) stringArray[rowNum, colNum])
                            .ToArray();

I'm not sure that this is really any better/simpler than a foreach loop though - particularly if you write an ExtractRow method and an ExtractColumn method and reuse them.

like image 55
Jon Skeet Avatar answered Sep 20 '22 12:09

Jon Skeet