c#简单实现二维数组和二维数组列表List<>的转置
刚看到网上一篇文章里用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<>的转置的更多相关文章
- homework-02 二维的,好喝的(二维数组的各种子数组)
1)输入部分 对于输入部分,我定义的输入格式是这样的 前两行为列数和行数 如果文件无法打开,或者输入文件格式不对,均会提示出错并退出 2)二维数组的最大矩形子数组 首先,我使用最最简单的暴力算法,直接 ...
- c++ 依据输入动态声明数组(一维,二维)
较早的编译器是不同意这样做的,所以一些书籍比方以Tc解说的书本都说数组的下标不能是变量.在vc6.0下亦是如此. 只是在一些较新的编译器如dev c++已经支持了,例如以下代码不会报错 #includ ...
- C语言数组:C语言数组定义、二维数组、动态数组、字符串数组
1.C语言数组的概念 在<更加优美的C语言输出>一节中我们举了一个例子,是输出一个 4×4 的整数矩阵,代码如下: #include <stdio.h> #include &l ...
- 【Java学习笔记之八】java二维数组及其多维数组的内存应用拓展延伸
多维数组声明 数据类型[][] 数组名称; 数据类型[] 数组名称[]; 数据类型数组名称[][]; 以上三种语法在声明二维数组时的功能是等价的.同理,声明三维数组时需要三对中括号,中括号的位置可以在 ...
- C++笔记-数组指针/二维数组转换指针
参考资料: 1. 作者 BensonLaur :https://www.cnblogs.com/BensonLaur/p/6367077.html 2. https://blog.csdn.net/ ...
- C#数组--(一维数组,二维数组的声明,使用及遍历)
数组:是具有相同数据类型的一组数据的集合.数组的每一个的变量称为数组的元素,数组能够容纳元素的数称为数组的长度. 一维数组:以线性方式存储固定数目的数组元素,它只需要1个索引值即可标识任意1个数组元素 ...
- 第3章 Java数组(上): 一维数组和二维数组
3.数组及排序算法(2天) 3.1 数组的概述 2课时 3.2 一维数组的使用 3课时 3.3 多维数组的使用 3课时 3.4 数组中涉及到的常见算法 3课时 3.5 Arrays工具类的使用 3课时 ...
- 二维数组转化为一维数组 contact 与apply 的结合
将多维数组(尤其是二维数组)转化为一维数组是业务开发中的常用逻辑,除了使用朴素的循环转换以外,我们还可以利用Javascript的语言特性实现更为简洁优雅的转换.本文将从朴素的循环转换开始,逐一介绍三 ...
- 二维数组 cudaMallocPitch() 和三维数组 cudaMalloc3D() 的使用
▶ 使用函数 cudaMallocPitch() 和配套的函数 cudaMemcpy2D() 来使用二维数组.C 中二维数组内存分配是转化为一维数组,连贯紧凑,每次访问数组中的元素都必须从数组首元素开 ...
随机推荐
- bootstrap 的可编辑下拉框 jquery.editable-select
搜了半天发现在某处下载jquery.editable-select需要积分,于是整理出来方便 其他人. 先上下载链接:http://pan.baidu.com/s/1kUXvwlL pass ...
- webservice接口国内手机号码归属地查询
操作步骤: 1.打开eclipse,新建web工程MobileCodeClient 2.打开运行窗口 右击工程名选择properties 切换到运行窗口,按图切换到相应的目录里 执行命令: wsdl2 ...
- 【转】wireshark基本用法及过虑规则
Wireshark 基本语法,基本使用方法,及包过虑规则: 1.过滤IP,如来源IP或者目标IP等于某个IP 例子: ip.src eq 192.168.1.107 or ip.dst eq 19 ...
- 平稳切换nginx版本
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- Scala 运算符和集合转换操作示例
Scala是数据挖掘算法领域最有力的编程语言之一,语言本身是面向函数,这也符合了数据挖掘算法的常用场景:在原始数据集上应用一系列的变换,语言本身也对集合操作提供了众多强大的函数,本文将以List类型为 ...
- Activiti 用户任务关联自定义表单
问题阐述 通常每一个"用户任务"都会对应一个表单,以供用户录入信息.尤其是在"流程定义"拥有多个版本的情形下,明确的指定表单显得极其重要. 一份新版本的&quo ...
- 使用binlog2sql做数据恢复的简单示例
有时我们会遇到操作人员误删或者误更新数据的情况,这时我们迫切希望把原来的数据还原回来,今天我们介绍一个简单的工具来方便的实现此功能. 前提条件 在实现数据恢复之前,需要我们的MySQL满足以下配置条件 ...
- (10.19)Java小作业
在java的学习过程中数组的版块也是十分重要的,包括一些教程也会在这个知识点花上更多的时间来讲解,足以证明 这个知识点的重要性,今天想和大家分享一道学习数组过程中不可避免的求最值题. 已知一个整形数组 ...
- sql server 2008 18456错误
来自:http://blog.csdn.net/qishuangquan/article/details/6024767 百度搜18456错误几乎只能搜到一篇文章,并不是说结果条数,而是所有的文章都是 ...
- OpenWRT 恢复出厂设置命令
如果通过无线或者有线口无法连接到router,可以用恢复某些设置重新设置路由器. 1. 开机,等着一个工作灯亮的时候立即按下rest键2秒,然后就开始拼命闪烁,很好现在进入failsafe模式了. 2 ...