c#中数组array和list在函数间传递 转置
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
double[,] array = new double[, ];
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
array[i, j] = i + j;
}
} //显示原数组
Console.WriteLine("Source Array:");
for (int i = ; i < ; i++)
{
string soureResult = string.Empty;
for (int j = ; j < ; j++)
{
soureResult += array[i, j] + " ";
}
Console.WriteLine(soureResult);
} double[,] newArray = Rotate(array);
//显示转置后的数组
Console.WriteLine("Destiney Array:");
for (int i = ; i < ; i++)
{
string dstResult = string.Empty;
for (int j = ; j < ; j++)
{
dstResult += newArray[i, j] + " ";
}
Console.WriteLine(dstResult);
} Console.ReadLine();
} public static double[,] Rotate(double[,] array)
{
int x = array.GetUpperBound(); //一维
int y = array.GetUpperBound(); //二维
double[,] newArray = new double[y + , x + ]; //构造转置二维数组
for (int i = ; i <= x; i++)
{
for (int j = ; j <= y; j++)
{
newArray[j, i] = array[i, j];
}
}
return newArray;
}
}
}
二维数组函数间传递及转置
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication2
{
class Program
{
/// <summary>
/// 二维数组转置函数
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
public static string[,] Rotate(string[,] array)
{
int x = array.GetUpperBound(); //一维
int y = array.GetUpperBound(); //二维
string[,] newArray = new string[y + , x + ]; //构造转置二维数组
for (int i = ; i <= x; i++)
{
for (int j = ; 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 == )
{
throw new IndexOutOfRangeException("Index OutOf Range");
}
int x = array[].Count;
int y = original.Count; //将列表抓换成数组
string[,] twoArray = new string[y, x];
for (int i = ; i < y; i++)
{
int j = ;
foreach (string s in array[i])
{
twoArray[i, j] = s;
j++;
}
} string[,] newTwoArray = new string[x, y];
newTwoArray = Rotate(twoArray);//转置 //二维数组转换成二维List集合
for (int i = ; i < x; i++)
{
List<string> list = new List<string>();
for (int j = ; 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 = ; i < ; i++)
{
List<string> list = new List<string>();
for (int j = ; j < ; j++)
{
list.Add(i.ToString() + j.ToString());
}
sourceList.Add(list);
} //显示原列表
Console.WriteLine("Source List:");
for (int i = ; i < sourceList.Count; i++)
{
string soureResult = string.Empty;
for (int j = ; 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 = ; i < dstList.Count; i++)
{
string dstResult = string.Empty;
for (int j = ; j < dstList[i].Count; j++)
{
dstResult += dstList[i][j] + " ";
}
Console.WriteLine(dstResult);
} Console.ReadLine();
}
}
}
list转置(通过数组中间变量)
参考:https://www.cnblogs.com/jeffwongishandsome/archive/2009/11/15/1603130.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication2
{
class Program
{
/// <summary>
/// 二维数组转置函数
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
static void Main(string[] args)
{
List<List<double>> twolist = new List<List<double>>();
List<double> onelist = new List<double>();
onelist.Add(11.0);
onelist.Add(12.0);
onelist.Add(13.0);
twolist.Add(onelist);
onelist[] = 21.0;//这样会将上面的11也抹去,可见这是地址引用
onelist[] = 22.0;
onelist[] = 23.0;
twolist.Add(onelist); //onelist .AddRange (new List<double> {21.0,22.0,23.0});
//twolist.AddRange(onelist);
double[,] two = twoDimenListToArray(twolist); }
public static double[,] twoDimenListToArray(List<List<double>> twoDimenList)
{ List<double>[] array = twoDimenList.ToArray();//将twoDimenList转换为一维list,list元素为数组
if (array.Length == )//array这个一维list(元素为数组)有几个数组元素
{
throw new IndexOutOfRangeException("Index OutOf Range");
}
int x = array[].Count;//array这个一维list(元素为数组)第一个元素(数组)内有几个元素(double类型数据),相当于列数
int y = twoDimenList.Count;//二维list中有几个list,相当于行数
double[,] twoDimenArray = new double[y, x];
for (int i = ; i < y; i++)//先写行
{
int j = ;
foreach (double d in array[i])
{
twoDimenArray[i, j] = d;
j++;
}
}
return twoDimenArray;
} }
}
将二维list转换为二维数组
修改:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication2
{
class Program
{
/// <summary>
/// 二维数组转置函数
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
static void Main(string[] args)
{
List<List<double>> twolist = new List<List<double>>();
List<double> onelist = new List<double>();
onelist.Add(11.0);
onelist.Add(12.0);
onelist.Add(13.0);
twolist.Add(onelist);
List<double> onelist1 = new List<double>();
onelist1.Add(21.0);//这样会将上面的11也抹去,可见这是地址引用
onelist1.Add(22.0) ;
onelist1.Add(23.0);
twolist.Add(onelist1); //onelist .AddRange (new List<double> {21.0,22.0,23.0});
//twolist.AddRange(onelist);
double[,] two = twoDimenListToArray(twolist); }
public static double[,] twoDimenListToArray(List<List<double>> twoDimenList)
{ List<double>[] array = twoDimenList.ToArray();//将twoDimenList转换为一维list,list元素为数组
if (array.Length == )//array这个一维list(元素为数组)有几个数组元素
{
throw new IndexOutOfRangeException("Index OutOf Range");
}
int x = array[].Count;//array这个一维list(元素为数组)第一个元素(数组)内有几个元素(double类型数据),相当于列数
int y = twoDimenList.Count;//二维list中有几个list,相当于行数
double[,] twoDimenArray = new double[y, x];
for (int i = ; i < y; i++)//先写行
{
int j = ;
foreach (double d in array[i])
{
twoDimenArray[i, j] = d;
j++;
}
}
return twoDimenArray;
} }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication2
{
class Program
{
/// <summary>
/// 二维数组转置函数
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
static void Main(string[] args)
{
List<List<double>> twolist = new List<List<double>>();
List<double> onelist = new List<double>();
onelist.Add(11.0);
onelist.Add(12.0);
onelist.Add(13.0);
twolist.Add(onelist);
List<double> onelist1 = new List<double>();
onelist1.Add(21.0);//这样会将上面的11也抹去,可见这是地址引用
onelist1.Add(22.0) ;
onelist1.Add(23.0);
twolist.Add(onelist1); //onelist .AddRange (new List<double> {21.0,22.0,23.0});
//twolist.AddRange(onelist);
double[,] two = twoDimenListToArray(twolist);
double[] one=oneDimenListToArray(onelist); }
/// <summary>
/// 将二维list转换为二维数组
/// </summary>
/// <param name="twoDimenList">传入的要转换的二维list列表</param>
/// <returns>返回转换得到的二维数组</returns>
public static double[,] twoDimenListToArray(List<List<double>> twoDimenList)
{ List<double>[] array = twoDimenList.ToArray();//将twoDimenList转换为一维list,list元素为数组
if (array.Length == )//array这个一维list(元素为数组)有几个数组元素
{
throw new IndexOutOfRangeException("Index OutOf Range");
}
int x = array[].Count;//array这个一维list(元素为数组)第一个元素(数组)内有几个元素(double类型数据),相当于列数
int y = twoDimenList.Count;//二维list中有几个list,相当于行数
double[,] twoDimenArray = new double[y, x];
for (int i = ; i < y; i++)//先写行
{
int j = ;
foreach (double d in array[i])
{
twoDimenArray[i, j] = d;
j++;
}
}
return twoDimenArray;
}
/// <summary>
/// 将一维list转换为一维数组
/// </summary>
/// <param name="oneDimenList">传入的要转换的一维list列表</param>
/// <returns>返回转换得到的一维数组</returns>
public static double[] oneDimenListToArray(List<double> oneDimenList)
{
double[] oneDimenArray = oneDimenList.ToArray();
return oneDimenArray;
} }
}
添加一维的转换
c#中数组array和list在函数间传递 转置的更多相关文章
- JavaScript中数组Array方法详解
ECMAScript 3在Array.prototype中定义了一些很有用的操作数组的函数,这意味着这些函数作为任何数组的方法都是可用的. 1.Array.join()方法 Array.join()方 ...
- C#中数组Array、ArrayList、泛型List<T>的比较
在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析. 一.数组Array 数组是一个存储相同类型元素的固定大小的顺序 ...
- JavaScript学习总结(三、函数声明和表达式、this、闭包和引用、arguments对象、函数间传递参数)
一.函数声明和表达式 函数声明: function test() {}; test(); //运行正常 function test() {}; 函数表达式: var test = functio ...
- C语言中数组名作为参数进行函数传递
用数组名作函数参数与用数组元素作实参有几点不同. 1) 用数组元素作实参时,只要数组类型和函数的形参变量的类型一致,那么作为下标变量的数组元素的类型也和函数形参变量的类型是一致的.因此,并不要求函数的 ...
- javascript中数组Array的方法
一.常用方法(push,pop,unshift,shift,join)push pop栈方法,后进先出var a =[1,2,3];console.log(a.push(40)); //4 返回数组的 ...
- JavaScript中数组Array.sort()排序方法详解
JavaScript中数组的sort()方法主要用于对数组的元素进行排序.其中,sort()方法有一个可选参数.但是,此参数必须是函数. 数组在调用sort()方法时,如果没有传参将按字母顺序(字符编 ...
- shell脚本中数组array常用技巧学习实践
shell中数组的下标默认是从0开始的 1.将字符串放在数组中,获取其长度 #!/bin/bashstr="a b --n d"array=($str)length=${#arra ...
- JAVA中数组Array与List互转
List<String> list = new ArrayList<String>();String[] array = new String[10]; 1.数组转成Listl ...
- JS中数组Array的用法{转载}
js数组元素的添加和删除一直比较迷惑,今天终于找到详细说明的资料了,先给个我测试的代码^-^var arr = new Array();arr[0] = "aaa";arr[1] ...
随机推荐
- 民生银行十五年的数据体系建设,深入解读阿拉丁大数据生态圈、人人BI 是如何养成的?【转】
早在今年的上半年我应邀参加了由 Smartbi 主办的一个小型数据分析交流活动,在活动现场第一次了解到了民生银行的阿拉丁项目.由于时间关系,嘉宾现场分享的内容非常有限.凭着多年对行业研究和对解决方案的 ...
- EasyUI 在Tab页用button打开新Tab
标签: <a href="javascript:void(0)" class="easyui-linkbutton" id="ipConfig& ...
- Win7 IIS 配置错误:不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的
因为 IIS 7 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项不允许更改.运行命令行 %windir%\system32\inetsrv\appcmd unlock conf ...
- java 基础之--nio 网络编程
在传统的Java 网络编程中,对于客户端的每次连接,对于服务器来说,都要创建一个新的线程与客户端进行通讯,这种频繁的线程的创建,对于服务器来说,是一种巨大的损耗,在Java 1.4 引入Java ni ...
- c#引用命名空间的作用
System 包含用于定义常用值和引用数据类型.事件和事件处理程序.接口.属性和处理异常的基础类和基类.其他类提供支持下列操作的服务:数据类型转换,方法参数操作,数学计算,远程和本地程序调用,应用程序 ...
- ubuntu关闭服务需要身份验证
service tomcat stop ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units === 需要通过认证才能停止“tom ...
- C语言中简单的for循环和浮点型变量
浮点型变量:常数中带有小数点的叫做浮点型 以下用for循环写一个摄氏度和华氏度的转换的C程序 [见 http://www.linuxidc.com/Linux/2013-08/88513.htm ] ...
- js 正则表达式:密码必须由6-12位数字加字母组成
^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$
- 20172306《Java程序设计与数据结构》第九周学习总结
20172306<Java程序设计>第九周学习总结 教材学习内容总结 第十一章: try-catch语句.其中还有finally语句.try是进行某些操作,catch是捕获异常,并通过某些 ...
- POJ 2449Remmarguts' Date 第K短路
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 29625 Accepted: 8034 ...