c#中数组array和list在函数间传递 转置

时间:2021-07-31 13:57:32
 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


c#中数组array和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);
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在函数间传递 转置的更多相关文章

  1. JavaScript中数组Array方法详解

    ECMAScript 3在Array.prototype中定义了一些很有用的操作数组的函数,这意味着这些函数作为任何数组的方法都是可用的. 1.Array.join()方法 Array.join()方 ...

  2. C&num;中数组Array、ArrayList、泛型List&lt&semi;T&gt&semi;的比较

    在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析. 一.数组Array 数组是一个存储相同类型元素的固定大小的顺序 ...

  3. JavaScript学习总结(三、函数声明和表达式、this、闭包和引用、arguments对象、函数间传递参数)

    一.函数声明和表达式 函数声明: function test() {}; test();    //运行正常 function test() {}; 函数表达式: var test = functio ...

  4. C语言中数组名作为参数进行函数传递

    用数组名作函数参数与用数组元素作实参有几点不同. 1) 用数组元素作实参时,只要数组类型和函数的形参变量的类型一致,那么作为下标变量的数组元素的类型也和函数形参变量的类型是一致的.因此,并不要求函数的 ...

  5. javascript中数组Array的方法

    一.常用方法(push,pop,unshift,shift,join)push pop栈方法,后进先出var a =[1,2,3];console.log(a.push(40)); //4 返回数组的 ...

  6. JavaScript中数组Array&period;sort&lpar;&rpar;排序方法详解

    JavaScript中数组的sort()方法主要用于对数组的元素进行排序.其中,sort()方法有一个可选参数.但是,此参数必须是函数. 数组在调用sort()方法时,如果没有传参将按字母顺序(字符编 ...

  7. shell脚本中数组array常用技巧学习实践

    shell中数组的下标默认是从0开始的 1.将字符串放在数组中,获取其长度 #!/bin/bashstr="a b --n d"array=($str)length=${#arra ...

  8. JAVA中数组Array与List互转

    List<String> list = new ArrayList<String>();String[] array = new String[10]; 1.数组转成Listl ...

  9. JS中数组Array的用法&lbrace;转载&rcub;

    js数组元素的添加和删除一直比较迷惑,今天终于找到详细说明的资料了,先给个我测试的代码^-^var arr = new Array();arr[0] = "aaa";arr[1] ...

随机推荐

  1. 【CCL】连通区域提取

    根据朋友给的一份原理写的 感觉还挺清楚 #include "cv.h" #include "highgui.h" #include <stdio.h&gt ...

  2. RPC远程过程调用协议

    最近学习Hadoop.Hbase.Spark及Storm原理,经常会出现RPC这样的传输术语,为了更好地理解,将知识点详细的整理下吧~ RPC-----它是一种通过网络从远程计算机程序上请求服务,而不 ...

  3. 201521123072《Java程序设计》第6周学习总结

    201521123072<Java程序设计>第6周学习总结 标签(空格分隔): java 1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画 ...

  4. 跨平台 webapp 开发技术之 Hybrid App

    前所知的 APP 开发模式有三种: 基于操作系统运行的 APP -> Native App,侧重于原生开发,用户体验好,需要安装才会升级 基于浏览器运行的 APP -> Web App,侧 ...

  5. 迅雷磁力链接转BT种子工具

    种子文件目录:C:\Users\jifeng\AppData\Local\Temp\magnetex MagnetEx.exe 从迅雷5.8支持磁力链接的无视受限资源版提取 MagnetEx.exe ...

  6. one by one 项目 part 1

    今天安装MySQL,我的系统是win8.1,安装包是mysql-5.7.17-winx64.zip,遇到了不少问题,特在此总结,希望能帮到遇到同样情况的人. 1.前面按照网上教程,先解压,然后在cmd ...

  7. Elasticsearch部分节点不能发现集群&lpar;脑裂&rpar;问题处理

    **现象描述** es1,es2,es3三台es组成一个集群,集群状态正常, 当es1 服务器重启后,es1不能加到集群中,自己选举自己为master,这就产生了es集群中所谓的“脑裂” , 把es1 ...

  8. jQuery制作鼠标经过显示图片大图,生成图片tips效果

    一般tips都是文字,这个可以支持图片,很漂亮: 演示   <script type="text/javascript"> // Load this script on ...

  9. Esxi5-管理平台vcenter5&period;0&lowbar;数据库迁移流程

    migrating-vcenter-database-express-to-sql-2008-r2 一.      准备环境. ESXi5.0主机      IP:192.168.1.158      ...

  10. PHP学习笔记(12)分页技术

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...