Convert 2 dimensional array

27,942

Solution 1

If you mean a jagged array (T[][]), SelectMany is your friend. If, however, you mean a rectangular array (T[,]), then you can just enumerate the date data via foreach - or:

int[,] from = new int[,] {{1,2},{3,4},{5,6}};
int[] to = from.Cast<int>().ToArray();

Solution 2

SelectMany is a projection operator, an extension method provided by the namespace System.Linq.

It performs a one to many element projection over a sequence, allowing you to "flatten" the resulting sequences into one.

You can use it in this way:

int[][] twoDimensional = new int[][] { 
                                      new int[] {1, 2},
                                      new int[] {3, 4},
                                      new int[] {5, 6}
                                     };

int [] flattened = twoDimensional.SelectMany(x=>x).ToArray();
Share:
27,942

Related videos on Youtube

Manoj
Author by

Manoj

Updated on August 27, 2020

Comments

  • Manoj
    Manoj over 3 years

    What is selectMany.ToArray() method? Is it a built in method in C#?

    I need to convert two dimensional array to one dimensional array.

  • Marc Gravell
    Marc Gravell about 15 years
    Pedant point; that isn't what I'd call a 2-dimensional array - it is a jagged array...
  • ShawnFeatherly
    ShawnFeatherly about 8 years
    This thread, stackoverflow.com/questions/5132397/… , goes into details of doing the foreach method and also some performance metrics.
  • barlop
    barlop about 8 years
    @MarcGravell Isn't it a 2D jagged array? Clearly a jagged array is not a rectangular array or "cube" array. But it still has a degree of dimension. To say 2D array doesn't specify jagged or geometric/straight.
  • Marc Gravell
    Marc Gravell about 8 years
    @barlop perhaps, but ultimately "the array" here would refer to to outer array, which is a vector of references; the jaggedness indeed makes it clear that it is more complex, and I would agree that in some ways "2D" could be used, as opposed to, say, an int[][][]. Ultimately, naming is hard :)
  • barlop
    barlop about 8 years
    @MarcGravell not sure why you bring up int[][][]. I'm not really talking about different ways he could've implemented something. All i'm saying is that an array of an array, is a 2 dimensional array, even if [the] array of array is jagged. When I mentioned cube, it was a bit unnecessary, but my point was that a 3D jagged array isn't a cube but it's still 3 dimensions. Just as a 2D jagged array isn't a rectangular array, but it still has 2 dimensions. Calling it 2D doesn't preclude it from being jagged. I am also talking about how we refer to that outter array as you are too.
  • Marc Gravell
    Marc Gravell about 8 years
    @barlop and technically, that outer array that we are both talking about: is a 1-dimensional vector (of references which just happen to themselves also be vectors). Sorry, but it is. That's just a statement of fact.
  • barlop
    barlop about 8 years
    @MarcGravell yes i am not denying that. Memory is linear, 1D, perhaps arguably 2D. But definitely not ever 3D. The concept of a multidimensional array is an abstraction. I don't know how a [,] or [,,,]` array is stored, but even if it's not stored as array of array, that point about multi dimensional arrays being an abstraction is also the case for those rectangular or 'cube' arrays too. You are using terms that are based on abstract concepts, like jagged array, and your mention of 2D array suggests you accept that abstract terminology. So I don't know why you are mentioning what you are
  • Marc Gravell
    Marc Gravell about 8 years
    @barlop because even in the abstraction: "the array" (the outer one) is notionally 1D, and is accessed as such. The fact that the things it contains are references to arrays doesn't change that.
  • barlop
    barlop about 8 years
  • Adrian Pop
    Adrian Pop over 5 years
    How does this exactly relates to the question: convert two dimensional array to one dimensional array?
  • iperov
    iperov over 5 years
    using 2d array over 1d array class, then you can easily "convert" to 1d array without performance drawback
  • iperov
    iperov over 5 years
    change to 2d is quite easy
  • Mike
    Mike about 5 years
    I had no idea that int[,] gets converted into IEnumerable<int> by Cast(). Cool trick!
  • Chmielok
    Chmielok about 4 years
    Keep in mind that if from is readonly, this method will fail.
  • Marc Gravell
    Marc Gravell about 4 years
    @Chmielok do you an example of what you mean there? if from is a readonly field, it should work fine; and AFAIK ImmutableArray<T> only supports vectors; so: what scenario are you describing that fails?