[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Convert between ragged arrays and two-dimensional arrays

[Convert between ragged arrays and two-dimensional arrays]

Ragged arrays are arrays that hold other arrays, as opposed to two-dimensional arrays. They are called "ragged arrays" because they work like two-dimensional arrays (or higher-dimensional arrays) where each row in the array can hold a different number of elements.

Converting between ragged arrays and two-dimensional arrays isn't too hard, it just requires some bookkeeping. You can also use generic parameters to make a method to convert between ragged arrays and two-dimensional arrays holding any type of objects.

The following extension method converts from a two-dimensional array to a ragged array.

// Convert T[,] to T[][]. public static T[][] ToRagged<T>(this T[,] values) { // Get the number of rows and columns. int num_rows = values.GetUpperBound(0) + 1; int num_cols = values.GetUpperBound(1) + 1; // Make the ragged array. T[][] result = new T[num_rows][]; // Copy values into the ragged array. for (int r = 0; r < num_rows; r++) { result[r] = new T[num_cols]; for (int c = 0; c < num_cols; c++) result[r][c] = values[r, c]; } return result; }

The method first gets the two-dimensional array's dimensions. It then creates a one-dimensional array to hold the ragged array's rows.

The code then loops through the two-dimensional array's rows. For each row, the method creates a one-dimensional array to hold the row's values and then copies the values into the row.

The following extension method converts a ragged array into a two-dimensional array.

// Convert T[][] to T[,]. public static T[,] To2DArray<T>(this T[][] values) { // Get the number of rows. int num_rows = values.GetUpperBound(0) + 1; // Get the maximum number of columns in any row. int num_cols = 0; for (int r = 0; r < num_rows; r++) if (num_cols < values[r].Length) num_cols = values[r].Length; // Make the two-dimensional array. T[,] result = new T[num_rows, num_cols]; // Copy values into the ragged array. for (int r = 0; r < num_rows; r++) { for (int c = 0; c < values[r].Length; c++) result[r, c] = values[r][c]; } return result; }

This method gets the number of rows in the ragged array. It then loops through its rows to see how long its longest row is.

The code then allocates a two-dimensional array with the correct number of rows and columns. It then loops through the ragged array's values adding them to the two-dimensional array.

Note that any entries in the two-dimensional array that do not correspond to entries in the ragged array are left uninitialized. For example, suppose the longest row in the ragged array has 25 entries but the first row has only 10 entries. In that case, the last 15 entries in the first row of the result array will hold only 10 values and the last 15 values will be uninitialized and will holdd the default value for their data type.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.