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

后端 未结 3 1016
暖寄归人
暖寄归人 2020-12-11 08:08

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 rever

3条回答
  •  情深已故
    2020-12-11 08:40

    I have written 2 methods which achieve the desired result

            /// 
            /// Converts the contents of a multidimensional array into a Jagged Array
            /// 
            /// the existing multidimensional array you wish to convert
            /// number of columns
            /// number of rows
            /// Jagged Array representation of multidimensional array passed
            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;
            }
    
    
    
        /// 
        /// Converts the contents of a Jagged Array into a multidimensional array
        /// 
        /// The Jagged Array you wish to convert into a Multidimensional Array
        /// number of columns
        /// number of rows
        /// Multidimensional Array representation of Jagged Array passed
        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");
                }
            }
    

提交回复
热议问题