jagged arrays <-> multidimensional arrays conversion in ASP.NET

北战南征 提交于 2019-11-27 07:13:38

问题


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?


回答1:


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;
    }



回答2:


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");
            }
        }



回答3:


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;
    }


来源:https://stackoverflow.com/questions/3010219/jagged-arrays-multidimensional-arrays-conversion-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!