I need help.
I have a job. In this specific case, I need to find the levels inside an array and I'm not sure how to do it.
Ex.: An array of 50 elements, for each one I have more X elements, for any X elements I have 'n' elements and inside of 'n' elements I can have more 'n' elements.
Array[0]
..elementOfArray[0]
.....elementOfElementArray[0]
..........NPossibilitiesOfElements[n]
....
.....elementOfElementArray[1]
..elementOfArray[1]
.....elementOfElementArray[0]
..........NPossibilitiesOfElements[n]
....
.....elementOfElementArray[1]
Array[1]
....
Array[2]
....
Anyone pass for this problem? How to solve this? Because I don't have the specific number of levels... and I didn't see a solution for this case.
I appreciate anyone who can help find a light at the end of the tunnel.
Let's get some terminology down.
C# supports two kinds of arrays:
string[m,n]
string[m][n]
Apparently, the kind of array you have is a jagged array. This is essentially just a single-dimensional array where each element is in turn another array. You can verify this by invoking array.GetType().GetArrayRank()
; it should return 1.
Apparently what you want to find is the length of your array along each dimension, and possibly also its true rank (how many dimensions it actually has).
The length of a single-dimension array can be obtained with array.GetLength( 0 )
.
Elements of a single-dimension array are in turn arrays if array.GetElementType().IsArray
is true.
So, to move to the next level, you need:
array[i].GetLength( 0 );
Here is a function that will find the rank of a jagged array, assuming that it is "normal", meaning that each nested array is of the same length as all other nested arrays at the same level of nesting:
using Sys = System;
public static int GetJaggedRank( Sys.Array jagged ) => GetJaggedRank( jagged.GetType() );
public static int GetJaggedRank( Sys.Type type )
{
Assert( type.IsArray ); //guaranteed to succeed.
Assert( type.GetArrayRank() == 1 ); //the array must be jagged, not multi-dimensional.
for( int i = 1;; i++ )
{
type = type.GetElementType()!;
if( !type.IsArray || type.GetArrayRank() != 1 )
return i;
}
}
Here is a function that will find the length of a jagged array for a given dimension:
public static int GetJaggedLength( Sys.Array jagged, int dimension )
{
Assert( jagged.Rank == 1 ); //the array must be jagged, not multi-dimensional.
Assert( dimension >= 0 && dimension < GetJaggedRank( jagged ) ); //the dimension must be between 0 and the rank of the jagged array.
if( jagged.GetLength( 0 ) > 0 )
{
for( ; dimension > 0; dimension-- )
{
jagged = (Sys.Array)jagged.GetValue( 0 );
Assert( jagged.Rank == 1 ); //nested array must also be a jagged array
}
}
return jagged.GetLength( 0 );
}
And here is a function that will find the length of each dimension in a jagged array up until a certain rank:
public static int[] GetJaggedLengths( Sys.Array jagged, int rank )
{
Assert( jagged.Rank == 1 ); //the array must be jagged, not multi-dimensional.
Assert( rank == GetJaggedRank( jagged ) ); //the given rank must be the correct rank.
int[] lengths = new int[rank];
for( int dimension = 0; dimension < rank; dimension++ )
lengths[dimension] = GetJaggedLength( jagged, dimension );
return lengths;
}