刚看到网上一篇文章里用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. MySQL Windows版安装详解

    一.下载MySQL MySQL官网https://dev.mysql.com提供了Windows下的安装版msi和解压版zip,其中均包含32和64位版本,mis版本与SqlServer安装基本一致N ...

  2. M-定在下边的区域

    1 效果 2 布局 3 样式

  3. zoj 1874 水题,输出格式大坑

    Primary Arithmetic Time Limit: 2 Seconds      Memory Limit: 65536 KB Children are taught to add mult ...

  4. nginx + keepalived 实现高可靠web网站

    组网图: 配置信息: 左边nigx 服务器的 /usr/local/nginx/conf/nginx.conf #user  nobody; worker_processes  1; #error_l ...

  5. Android Annotations Eclipse 配置 (3)

    Android Annotations 本来我想写个 Java 版本的<RESTful客户端库:RestClient>用于 Android 应用开发,结果发现不太好写,虽然用了 Dynam ...

  6. 【JDK1.8】JDK1.8集合源码阅读——总章

    一.前言 今天开始阅读jdk1.8的集合部分,平时在写项目的时候,用到的最多的部分可能就是Java的集合框架,通过阅读集合框架源码,了解其内部的数据结构实现,能够深入理解各个集合的性能特性,并且能够帮 ...

  7. UVa1630,Folding

    区间dp,记忆化搜就可以 st为原串 dp[p][q]存st[p]~st[q]的最优长度,f[p][q]存对应的最优串 从(0,len-1)开始搜,f[0][len-1]为所求ans,回溯条件为p== ...

  8. 多线程之Map:Hashtable HashMap 以及ConcurrentHashMap

    1.Map体系参考:http://java.chinaitlab.com/line/914247.htmlHashtable是JDK 5之前Map唯一线程安全的内置实现(Collections.syn ...

  9. 【Win 10 应用开发】在代码中加载文本资源

    记得前一次,老周给大伙,不,小伙伴们介绍了如何填写 .resw 文件,并且在 XAML 中使用 x:Uid 标记来加载.也顺便给大伙儿分析了运行时是如何解析 .resw 文件的. 本来说好了,后续老周 ...

  10. python学习笔记(一)之入门

    1.python的安装 官网下载.exe文件直接安装即可,在安装过程中选择加入环境变量,就不用在安装后再去增添环境变量了. 本文选择的是python3.6版本,没有选择2.7版本. 2.启动pytho ...