4

I would like some help to create the following convertions:

A need to convert an 800*600 multidimensional array into a jagged array and then the same method in reverse (jagged array with the same data to the original multidimensional array)

Is this possible? and any help about it?

3 Answers 3

4

I realize the question is a bit old but for flexability sake, I modfied the method a bit to calculate the array sizes within the method as opposed to having to pass them in:

    static object[][] convertToJaggedArray(object[,] multiArray)
    {
        int firstElement = multiArray.GetLength(0);
        int secondElement = multiArray.GetLength(1);

        object[][] jaggedArray = new object[firstElement][];

        for (int c = 0; c < firstElement; c++)
        {
            jaggedArray[c] = new object[secondElement];
            for (int r = 0; r < secondElement; r++)
            {
                jaggedArray[c][r] = multiArray[c, r];
            }
        }
        return jaggedArray;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

I have written 2 methods which achieve the desired result

        /// <summary>
        /// Converts the contents of a multidimensional array into a Jagged Array
        /// </summary>
        /// <param name="multiArray">the existing multidimensional array you wish to convert</param>
        /// <param name="numOfColumns">number of columns</param>
        /// <param name="numOfRows">number of rows</param>
        /// <returns>Jagged Array representation of multidimensional array passed</returns>
        private int[][] convertToJaggedArray(int [,] multiArray, int numOfColumns, int numOfRows)
        {
            int[][] jaggedArray = new int[numOfColumns][];

            for (int c = 0; c < numOfColumns; c++)
            {
                jaggedArray[c] = new int[numOfRows];
                for (int r = 0; r < numOfRows; r++)
                {
                    jaggedArray[c][r] = multiArray[c, r];
                }
            }

            return jaggedArray;
        }



    /// <summary>
    /// Converts the contents of a Jagged Array into a multidimensional array
    /// </summary>
    /// <param name="jaggedArray">The Jagged Array you wish to convert into a Multidimensional Array</param>
    /// <param name="numOfColumns">number of columns</param>
    /// <param name="numOfRows">number of rows</param>
    /// <returns>Multidimensional Array representation of Jagged Array passed</returns>
    private int[,] convertTo2DArray(int[][] jaggedArray, int numOfColumns, int numOfRows)
    {
        int[,] temp2DArray = new int[numOfColumns, numOfRows];

        for (int c = 0; c < numOfColumns; c++)
        {
            for (int r = 0; r < numOfRows; r++)
            {
                temp2DArray[c, r] = jaggedArray[c][r];
            }
        }

        return temp2DArray;
    } 

then you simply pass your existing multidimensional or jagged array and it will return the same contents in a different array type. e.g.

//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);

You can also check and print out the contents of either by using the following loops

// Print out all elements in the jagged array to debugger.
        for (int c = 0; c < jaggedGrid.Length; c++)
        {
            int[] innerArray = jaggedGrid[c];
            for (int r = 0; r < 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 < temp.GetLength(0); c++)
        {
            for (int r = 0; r < temp.GetLength(1); r++)
            {
                System.Diagnostics.Debug.WriteLine("In temp array\nElement No ({0},{1})={2}", c, r, temp[c, r] + "\n");
            }
        }

Comments

1

I made few changes as Multiarray starts from index 1 while Jagged array starts from index 0.

static object[][] convertToJaggedArray(object[,] multiArray, int firstElement, int secondElement)
    {
        object[][] jaggedArray = new object[firstElement][];

        for (int c = 0; c < firstElement; c++)
        {
            jaggedArray[c] = new object[secondElement];
            for (int r = 0; r < secondElement; r++)
            {
                jaggedArray[c][r] = multiArray[c+1, r+1];
            }
        }
        return jaggedArray;
    }

1 Comment

Since when are multiarray indexes 1-based? The examples here start from 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.