龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

c#实现二维数组转置及二维数组列表List<>转置

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
面利用c#利器也实现一遍二维数组的转置 首先来看二维数组转置代码: Code [http://www.xueit.com] class Program{ public static string [,] Rotate( string [,] array) { int x = array.GetUpperBound( 0 ); // 一维 int y

面利用c#利器也实现一遍二维数组的转置

首先来看二维数组转置代码:

Code [http://www.xueit.com]
class Program
{
    public static string[,] Rotate(string[,] array)
    {
        int x = array.GetUpperBound(0); //一维 
        int y = array.GetUpperBound(1); //二维 
        string[,] newArray = new string[y   1, x   1]; //构造转置二维数组
        for (int i = 0; i <= x; i  )
        {
            for (int j = 0; j <= y; j  )
            { 
                newArray[j, i] = array[i, j]; 
            }
        } 
        return newArray;
    }

    static void Main(string[] args)
    {

        string[,] array = new string[4, 2];
        for (int i = 0; i < 4; i  )
        {
            for (int j = 0; j < 2; j  )
            {
                array[i, j] = i.ToString()   j.ToString();
            }
        }

        //显示原数组
        Console.WriteLine("Source Array:");
        for (int i = 0; i < 4; i  )
        {
            string soureResult = string.Empty;
            for (int j = 0; j < 2; j  )
            {
                soureResult  = array[i, j]   "  ";
            }
            Console.WriteLine(soureResult);
        }

        string[,] newArray = Rotate(array);
        //显示转置后的数组
        Console.WriteLine("Destiney Array:");
        for (int i = 0; i < 2; i  )
        {
            string dstResult = string.Empty;
            for (int j = 0; j < 4; j  )
            {
                dstResult  = newArray[i, j]   "  ";
            }
            Console.WriteLine(dstResult);
        }

        Console.ReadLine();
    }
}

好了,下面接着看二维数组列表List<>的转置


精彩图集

赞助商链接