This is a follow-up question for ConvertAll Methods Implementation for Multidimensional Array in C# and ConvertAll Methods Implementation for Multidimensional Array in C# - follow-up. Besides the multidimensional array ([,]
, [,,]
, [,,,]
...) case, I am trying to implement another series overloading methods to deal with jagged arrays in C#.
The experimental implementation
The experimental implementation is as below.
class Converters
{
public static TOutput[][] ConvertAll<TInput, TOutput>(TInput[][] inputs, Converter<TInput, TOutput> converter)
{
if (inputs is null)
{
throw new ArgumentNullException(nameof(inputs));
}
if (converter is null)
{
throw new ArgumentNullException(nameof(converter));
}
TOutput[][] output = Array.ConvertAll(inputs, dim1 => Array.ConvertAll(dim1, dim2 => converter(dim2)));
return output;
}
public static TOutput[][][] ConvertAll<TInput, TOutput>(TInput[][][] inputs, Converter<TInput, TOutput> converter)
{
if (inputs is null)
{
throw new ArgumentNullException(nameof(inputs));
}
if (converter is null)
{
throw new ArgumentNullException(nameof(converter));
}
TOutput[][][] output = Array.ConvertAll(inputs, dim1 =>
Array.ConvertAll(dim1, dim2 =>
Array.ConvertAll(dim2, dim3 => converter(dim3)
)));
return output;
}
public static TOutput[][][][] ConvertAll<TInput, TOutput>(TInput[][][][] inputs, Converter<TInput, TOutput> converter)
{
if (inputs is null)
{
throw new ArgumentNullException(nameof(inputs));
}
if (converter is null)
{
throw new ArgumentNullException(nameof(converter));
}
TOutput[][][][] output = Array.ConvertAll(inputs, dim1 =>
Array.ConvertAll(dim1, dim2 =>
Array.ConvertAll(dim2, dim3 =>
Array.ConvertAll(dim3, dim4 => converter(dim4)
))));
return output;
}
public static TOutput[][][][][] ConvertAll<TInput, TOutput>(TInput[][][][][] inputs, Converter<TInput, TOutput> converter)
{
if (inputs is null)
{
throw new ArgumentNullException(nameof(inputs));
}
if (converter is null)
{
throw new ArgumentNullException(nameof(converter));
}
TOutput[][][][][] output = Array.ConvertAll(inputs, dim1 =>
Array.ConvertAll(dim1, dim2 =>
Array.ConvertAll(dim2, dim3 =>
Array.ConvertAll(dim3, dim4 =>
Array.ConvertAll(dim4, dim5 => converter(dim5)
)))));
return output;
}
public static TOutput[][][][][][] ConvertAll<TInput, TOutput>(TInput[][][][][][] inputs, Converter<TInput, TOutput> converter)
{
if (inputs is null)
{
throw new ArgumentNullException(nameof(inputs));
}
if (converter is null)
{
throw new ArgumentNullException(nameof(converter));
}
TOutput[][][][][][] output = Array.ConvertAll(inputs, dim1 =>
Array.ConvertAll(dim1, dim2 =>
Array.ConvertAll(dim2, dim3 =>
Array.ConvertAll(dim3, dim4 =>
Array.ConvertAll(dim4, dim5 =>
Array.ConvertAll(dim5, dim6 => converter(dim6)
))))));
return output;
}
}
Test cases
The test cases listed here include array of arrays, array of arrays of arrays and array of arrays of arrays of arrays.
// Reference: https://www.geeksforgeeks.org/c-sharp-jagged-arrays/
Console.WriteLine("Jagged Array [][]");
Console.WriteLine();
int[][] jagged_arr = new int[][]
{
new int[] {1, 2, 3, 4},
new int[] {11, 34, 67},
new int[] {89, 23},
new int[] {0, 45, 78, 53, 99}
};
double[][] output1 = Converters.ConvertAll(jagged_arr, x => x + 0.1);
for (int dim1 = 0; dim1 < output1.Length; dim1++)
{
for (int dim2 = 0; dim2 < output1[dim1].GetLength(0); dim2++)
{
Console.Write($"[{dim1}][{dim2}]: {output1[dim1][dim2]}\t");
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("Jagged Array [][][]");
Console.WriteLine();
int[][][] jagged_arr2 = new int[][][]
{
jagged_arr,
jagged_arr,
jagged_arr,
jagged_arr
};
double[][][] output2 = Converters.ConvertAll(jagged_arr2, x => x + 0.1);
for (int dim1 = 0; dim1 < output2.Length; dim1++)
{
Console.WriteLine($"dim1 = {dim1}");
for (int dim2 = 0; dim2 < output2[dim1].GetLength(0); dim2++)
{
for (int dim3 = 0; dim3 < output2[dim1][dim2].GetLength(0); dim3++)
{
Console.Write($"[{dim2}][{dim3}]: {output2[dim1][dim2][dim3]}\t");
}
Console.WriteLine();
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("Jagged Array [][][][]");
Console.WriteLine();
int[][][][] jagged_arr3 = new int[][][][]
{
jagged_arr2,
jagged_arr2,
jagged_arr2,
jagged_arr2
};
double[][][][] output3 = Converters.ConvertAll(jagged_arr3, x => x + 0.1);
for (int dim1 = 0; dim1 < output3.Length; dim1++)
{
for (int dim2 = 0; dim2 < output3[dim1].GetLength(0); dim2++)
{
Console.WriteLine($"dim1 = {dim1}, dim2 = {dim2}");
for (int dim3 = 0; dim3 < output3[dim1][dim2].GetLength(0); dim3++)
{
for (int dim4 = 0; dim4 < output3[dim1][dim2][dim3].GetLength(0); dim4++)
{
Console.Write($"[{dim3}][{dim4}]: {output3[dim1][dim2][dim3][dim4]}\t");
}
Console.WriteLine();
}
Console.WriteLine();
}
}
The output of the test code above:
Jagged Array [][]
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
Jagged Array [][][]
dim1 = 0
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 1
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 2
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 3
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
Jagged Array [][][][]
dim1 = 0, dim2 = 0
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 0, dim2 = 1
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 0, dim2 = 2
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 0, dim2 = 3
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 1, dim2 = 0
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 1, dim2 = 1
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 1, dim2 = 2
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 1, dim2 = 3
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 2, dim2 = 0
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 2, dim2 = 1
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 2, dim2 = 2
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 2, dim2 = 3
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 3, dim2 = 0
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 3, dim2 = 1
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 3, dim2 = 2
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
dim1 = 3, dim2 = 3
[0][0]: 1.1 [0][1]: 2.1 [0][2]: 3.1 [0][3]: 4.1
[1][0]: 11.1 [1][1]: 34.1 [1][2]: 67.1
[2][0]: 89.1 [2][1]: 23.1
[3][0]: 0.1 [3][1]: 45.1 [3][2]: 78.1 [3][3]: 53.1 [3][4]: 99.1
All suggestions are welcome.
The summary information:
Which question it is a follow-up to?
ConvertAll Methods Implementation for Multidimensional Array in C# and
ConvertAll Methods Implementation for Multidimensional Array in C# - follow-up
What changes has been made in the code since last question?
Trying to implement another series
ConvertAll
overloading methods to deal with jagged arrays in C#.Why a new review is being asked for?
If there is any issue about potential drawback or unnecessary overhead of the implemented methods, please let me know.