What is selectMany.ToArray()
method? Is it a built in method in C#
?
I need to convert two dimensional array to one dimensional array.
What is selectMany.ToArray()
method? Is it a built in method in C#
?
I need to convert two dimensional array to one dimensional array.
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();
int[,]
gets converted into IEnumerable<int>
by Cast()
. Cool trick! –
Chafe from
is readonly, this method will fail. –
Landmass 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 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();
int[][][]
. Ultimately, naming is hard :) –
Siskind 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 [,]
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 –
Pinnatipartite 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
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;
}
}
convert two dimensional array to one dimensional array
? –
Tectonics © 2022 - 2024 — McMap. All rights reserved.