Convert 2 dimensional array
Asked Answered
S

4

29

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

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

Sequacious answered 13/3, 2009 at 4:46 Comment(0)
S
68

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();
Siskind answered 13/3, 2009 at 5:21 Comment(4)
This thread, #5132897 , goes into details of doing the foreach method and also some performance metrics.Aerodonetics
I had no idea that int[,] gets converted into IEnumerable<int> by Cast(). Cool trick!Chafe
Keep in mind that if from is readonly, this method will fail.Landmass
@Landmass 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?Siskind
H
32

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();
Horrify answered 13/3, 2009 at 4:48 Comment(8)
Pedant point; that isn't what I'd call a 2-dimensional array - it is a jagged array...Siskind
@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.Pinnatipartite
@Pinnatipartite 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 :)Siskind
@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.Pinnatipartite
@Pinnatipartite 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.Siskind
@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 arePinnatipartite
@Pinnatipartite 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.Siskind
Let us continue this discussion in chat.Pinnatipartite
R
1

This solution is devoted to convert any sort of int array, regular, jagged, or nested (these last are taken from javascript and its object notation, but they can also be implemented as complex jagged array of objects in C#), into a simple mono-dimensional integers array:

    public static int[] getFlattenedIntArray(object jaggedArray)
    {

        var flattenedArray = new List<int>();

        var jaggedArrayType = jaggedArray.GetType();
        var expectedType = typeof(int);

        if (jaggedArrayType.IsArray)
        {
            if (expectedType.IsAssignableFrom(jaggedArrayType.GetElementType()))
            {
                foreach (var el in jaggedArray as int[])
                {
                    flattenedArray.Add(el);
                }
            }
            else
            {
                foreach (var el in jaggedArray as object[])
                {
                    foreach (var retIntEl in getFlattenedIntArray(el))
                    {
                        flattenedArray.Add(retIntEl);
                    }
                }

            }
        }
        else if (jaggedArrayType == expectedType)
        {
            flattenedArray.Add((int)jaggedArray);
        }
        else
        {
            return new int[0];
        }

        return flattenedArray.ToArray();
    }

Try it with this fiddle: https://dotnetfiddle.net/5HGX96

Reverberation answered 24/8, 2020 at 14:34 Comment(0)
L
-1

my solution:

public struct Array3D<T>
{
    public T[] flatten;
    int x_len;
    int y_len;
    int z_len;

    public Array3D(int z_len, int y_len, int x_len)
    {
        this.x_len = x_len;
        this.y_len = y_len;
        this.z_len = z_len;
        flatten = new T[z_len * y_len * x_len];
    }

    public int getOffset(int z, int y, int x) => y_len * x_len * z + x_len * y + x;

    public T this[int z, int y, int x] {
        get => flatten[y_len * x_len * z + x_len * y + x];
        set => flatten[y_len * x_len * z + x_len * y + x] = value;
    }

    public T this[int flat_index] {
        get => flatten[flat_index];
        set => flatten[flat_index] = value;
    }
}
Leeuwenhoek answered 3/12, 2018 at 17:28 Comment(3)
How does this exactly relates to the question: convert two dimensional array to one dimensional array?Tectonics
using 2d array over 1d array class, then you can easily "convert" to 1d array without performance drawbackLeeuwenhoek
change to 2d is quite easyLeeuwenhoek

© 2022 - 2024 — McMap. All rights reserved.