刚看到网上一篇文章里用sql实现了行列转置。sql server 2005/2008只用一个pivot函数就可以实现sql server 2000很多行的复杂实现。提到转置,立刻想起还在求学阶段曾经做过的一个练习,用c语言实现二维数组的转置。相信大家都做过这个练习。下面利用c#利器也实现一遍,没有实际意义,练练手而已。

1、二维数组转置

Code 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();        }    }

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

Code 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();        }    }

c#简单实现二维数组和二维数组列表List&lt;&gt;的转置的更多相关文章

  1. homework-02 二维的,好喝的(二维数组的各种子数组)

    1)输入部分 对于输入部分,我定义的输入格式是这样的 前两行为列数和行数 如果文件无法打开,或者输入文件格式不对,均会提示出错并退出 2)二维数组的最大矩形子数组 首先,我使用最最简单的暴力算法,直接 ...

  2. c++ 依据输入动态声明数组(一维,二维)

    较早的编译器是不同意这样做的,所以一些书籍比方以Tc解说的书本都说数组的下标不能是变量.在vc6.0下亦是如此. 只是在一些较新的编译器如dev c++已经支持了,例如以下代码不会报错 #includ ...

  3. C语言数组:C语言数组定义、二维数组、动态数组、字符串数组

    1.C语言数组的概念 在<更加优美的C语言输出>一节中我们举了一个例子,是输出一个 4×4 的整数矩阵,代码如下: #include <stdio.h> #include &l ...

  4. 【Java学习笔记之八】java二维数组及其多维数组的内存应用拓展延伸

    多维数组声明 数据类型[][] 数组名称; 数据类型[] 数组名称[]; 数据类型数组名称[][]; 以上三种语法在声明二维数组时的功能是等价的.同理,声明三维数组时需要三对中括号,中括号的位置可以在 ...

  5. C++笔记-数组指针/二维数组转换指针

    参考资料: 1. 作者 BensonLaur  :https://www.cnblogs.com/BensonLaur/p/6367077.html 2. https://blog.csdn.net/ ...

  6. C#数组--(一维数组,二维数组的声明,使用及遍历)

    数组:是具有相同数据类型的一组数据的集合.数组的每一个的变量称为数组的元素,数组能够容纳元素的数称为数组的长度. 一维数组:以线性方式存储固定数目的数组元素,它只需要1个索引值即可标识任意1个数组元素 ...

  7. 第3章 Java数组(上): 一维数组和二维数组

    3.数组及排序算法(2天) 3.1 数组的概述 2课时 3.2 一维数组的使用 3课时 3.3 多维数组的使用 3课时 3.4 数组中涉及到的常见算法 3课时 3.5 Arrays工具类的使用 3课时 ...

  8. 二维数组转化为一维数组 contact 与apply 的结合

    将多维数组(尤其是二维数组)转化为一维数组是业务开发中的常用逻辑,除了使用朴素的循环转换以外,我们还可以利用Javascript的语言特性实现更为简洁优雅的转换.本文将从朴素的循环转换开始,逐一介绍三 ...

  9. 二维数组 cudaMallocPitch() 和三维数组 cudaMalloc3D() 的使用

    ▶ 使用函数 cudaMallocPitch() 和配套的函数 cudaMemcpy2D() 来使用二维数组.C 中二维数组内存分配是转化为一维数组,连贯紧凑,每次访问数组中的元素都必须从数组首元素开 ...

随机推荐

  1. PHP抓取网页图片

    <?php set_time_limit(0);//抓取不受时间限制 if($_POST['Submit']=="开始抓取"){ $URL=$_POST['link']; g ...

  2. 基于LoadRunner11,以wifi热点方式录制APP脚本简单指导

    本想详细写下操作过程,但并不觉着十分必要,通过baidu或我要自学网均能找到相关资料,所以详细操作过程不再赘述,只是把过程中遇到的问题说明下解释下,让大家“录制APP”的路更平坦! 1.如何使用Loa ...

  3. js X年X周 转成 具体日期

    function getWeekDate(theyear,weekcount) { var year = theyear; var week = weekcount; if(year=="& ...

  4. 干了这杯Java之LinkedList

    LinkedList和ArrayList一样实现了List接口 ArrayList内部为数组 LinkedList内外为双向链表 实现了Deque接口,双端列队的实现 图片来自Wiki 内部实现为No ...

  5. rewrap-ajax.js插件

    很久没有动手写技术的文章,这个过程中间一直在写日记,生活点滴的记录替代了技术文章的编写,可以看出以往的内心是激情或烈火,现在是... 最近写了一个JS插件,用圈内的话说叫造了个轮子,造的好与不好都不是 ...

  6. MySQL锁类型以及子查询锁表问题、解锁

    MySQL中select * for update锁表的范围 MySQL中select * for update锁表的问题 由于InnoDB预设是Row-Level Lock,所以只有「明确」的指定主 ...

  7. C# To JAVA Converter Cracked ( 破解版 )

    C# To JAVA Converter v17.10.6  Cracked by X-Cracker 简介 C# To Java converter是一款将C#代码片段或者C#项目转换为JAVA的工 ...

  8. Myeclipse常见快捷键及配置

    0. 快捷键 ================================================================================ 编辑: Ctrl+Shi ...

  9. 服务端事件EventSource揭秘

    服务端推 服务端推,指的是由服务器主动的向客户端发送消息(响应).在应用层的HTTP协议实现中,"请求-响应"是一个round trip,它的起点来自客户端,因此在应用层之上无法实 ...

  10. iOS 之 UITextField

    UITextField 相关细节处理: 1.  设置leftView , rightView let leftView = UIView() // 设置leftView/rightView之后,勿忘设 ...