Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have written 2 methods which achieve the desired result</p> <pre><code> /// &lt;summary&gt; /// Converts the contents of a multidimensional array into a Jagged Array /// &lt;/summary&gt; /// &lt;param name="multiArray"&gt;the existing multidimensional array you wish to convert&lt;/param&gt; /// &lt;param name="numOfColumns"&gt;number of columns&lt;/param&gt; /// &lt;param name="numOfRows"&gt;number of rows&lt;/param&gt; /// &lt;returns&gt;Jagged Array representation of multidimensional array passed&lt;/returns&gt; private int[][] convertToJaggedArray(int [,] multiArray, int numOfColumns, int numOfRows) { int[][] jaggedArray = new int[numOfColumns][]; for (int c = 0; c &lt; numOfColumns; c++) { jaggedArray[c] = new int[numOfRows]; for (int r = 0; r &lt; numOfRows; r++) { jaggedArray[c][r] = multiArray[c, r]; } } return jaggedArray; } /// &lt;summary&gt; /// Converts the contents of a Jagged Array into a multidimensional array /// &lt;/summary&gt; /// &lt;param name="jaggedArray"&gt;The Jagged Array you wish to convert into a Multidimensional Array&lt;/param&gt; /// &lt;param name="numOfColumns"&gt;number of columns&lt;/param&gt; /// &lt;param name="numOfRows"&gt;number of rows&lt;/param&gt; /// &lt;returns&gt;Multidimensional Array representation of Jagged Array passed&lt;/returns&gt; private int[,] convertTo2DArray(int[][] jaggedArray, int numOfColumns, int numOfRows) { int[,] temp2DArray = new int[numOfColumns, numOfRows]; for (int c = 0; c &lt; numOfColumns; c++) { for (int r = 0; r &lt; numOfRows; r++) { temp2DArray[c, r] = jaggedArray[c][r]; } } return temp2DArray; } </code></pre> <p>then you simply pass your existing multidimensional or jagged array and it will return the same contents in a different array type. e.g.</p> <pre><code>//1. convert to Jagged print out, grid 1 is the existing 2d array instance here int[][] jaggedGrid = convertToJaggedArray(grid1, numOfCols, numOfRows); //2. Take the jagged and re-convert to multi array int[,] temp = convertTo2DArray(jaggedGrid, numOfCols, numOfRows); </code></pre> <p>You can also check and print out the contents of either by using the following loops</p> <pre><code>// Print out all elements in the jagged array to debugger. for (int c = 0; c &lt; jaggedGrid.Length; c++) { int[] innerArray = jaggedGrid[c]; for (int r = 0; r &lt; innerArray.Length; r++) { System.Diagnostics.Debug.WriteLine("In Jagged Array\nElement No ({0},{1})={2}", c, r, jaggedGrid[c][r] + "\n"); } } //print out all values in temp value 2d array to debugger for (int c = 0; c &lt; temp.GetLength(0); c++) { for (int r = 0; r &lt; temp.GetLength(1); r++) { System.Diagnostics.Debug.WriteLine("In temp array\nElement No ({0},{1})={2}", c, r, temp[c, r] + "\n"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload