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

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

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
这个是想到在实际项目中操作集合经常用到泛型List,顺便实现一下它的转置。思路很简单,根据1,我们已经实现了转置,所以就要想方设法把泛型List转换

这个是想到在实际项目中操作集合经常用到泛型List,顺便实现一下它的转置。思路很简单,根据1,我们已经实现了转置,所以就要想方设法把泛型List转换成二维数组,然后转置,接着将转换后的数组再转换成泛型List。呵呵,笔者偷懒惯了,其实应该还有其他的方法,不管了,先实现看下效果。

Code [http://www.xueit.com]
class Program
{
    /// <summary>
    /// 二维数组转置函数
    /// </summary>
    /// <param name="array"></param>
    /// <returns></returns>
    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;
    }

    /// <summary>
    /// 将二维列表(List)转换成二维数组,二维数组转置,然后将二维数组转换成列表
    /// </summary>
    /// <param name="original"></param>
    /// <returns></returns>
    public static List<List<string>> Rotate(List<List<string>> original)
    {
        List<string>[] array = original.ToArray();
        List<List<string>> lists = new List<List<string>>();
        if (array.Length==0)
        {
            throw new IndexOutOfRangeException("Index OutOf Range");
        }
        int x = array[0].Count;
        int y = original.Count;

        //将列表抓换成数组
        string[,] twoArray = new string[y, x];
        for (int i = 0; i < y; i  )
        {
            int j = 0;
            foreach (string s in array[i])
            {
                twoArray[i, j] = s; 
                j  ;
            }
        }

        string[,] newTwoArray = new string[x, y];
        newTwoArray = Rotate(twoArray);//转置

        //二维数组转换成二维List集合
        for (int i = 0; i < x; i  )
        {
            List<string> list = new List<string>();
            for (int j = 0; j < y; j  )
            {
                list.Add(newTwoArray[i, j]);
            }
            lists.Add(list);
        }
        return lists;
    }

    static void Main(string[] args)
    {
        List<List<string>> sourceList = new List<List<string>>(); //测试的二维List
        for (int i = 0; i < 4; i  )
        {
            List<string> list = new List<string>();
            for (int j = 0; j < 2; j  )
            {
                list.Add(i.ToString()   j.ToString());
            }
            sourceList.Add(list);
        }

        //显示原列表
        Console.WriteLine("Source List:");
        for (int i = 0; i < sourceList.Count; i  )
        {
            string soureResult = string.Empty;
            for (int j = 0; j < sourceList[i].Count; j  )
            {
                soureResult  = sourceList[i][j]   "  ";
            }
            Console.WriteLine(soureResult);
        }

        List<List<string>> dstList = Rotate(sourceList);
        //显示转置后的列表
        Console.WriteLine("Destiney List:");
        for (int i = 0; i < dstList.Count; i  )
        {
            string dstResult = string.Empty;
            for (int j = 0; j < dstList[i].Count; j  )
            {
                dstResult  = dstList[i][j]   "  ";
            }
            Console.WriteLine(dstResult);
        }

        Console.ReadLine();
    }
}

精彩图集

赞助商链接