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] ...
随机推荐
- Cisco VSS
1.原理 VSS是将两台及以上的物理设备虚拟成逻辑上的一台,可类比堆叠.VSS在控制层面上两个交换机有主从之分,但在数据面上处理是双活的.无论是从网络控制层面和管理视图上在网络上都是一个单独的设备实体 ...
- iOS - 抖音效果
抖音的转场动画—iOS https://www.jianshu.com/p/29b0165de712 抖音的上下滑实现—iOS https://www.jianshu.com/p/e8799510c7 ...
- go语言使用go-sciter创建桌面应用(二) ui元素查找,增加,删除,修改
我们可以通过go-sciter给我们提供的方法,方便的对html,css编写的UI界面进行增删改查. demo3.go代码如下: package main; import ( "github ...
- for循环中的 break和continue的区别
break 语句用于跳出循环. for (i=0;i<10;i++) { if (i==3) { break; } x=x + "The number is " + i + ...
- 转::before和::after伪元素的用法
一.介绍 css3为了区分伪类和伪元素,伪元素采用双冒号写法. 常见伪类——:hover,:link,:active,:target,:not(),:focus. 常见伪元素——::first-let ...
- Android开发之使用GridView+仿微信图片上传功能(附源代码)
前言:如果转载文章请声明转载自:https://i.cnblogs.com/EditPosts.aspx?postid=7419021 .另外针对有些网站转载本人的文章结果源码链接不对的问题,本人在 ...
- 如何在Fragment中获取context
文章转载自http://blog.csdn.net/demonliuhui/article/details/51511136 这里仅供自己学习参考: Context,中文直译为“上下文”,SDK中对其 ...
- 操作系统之CPU管理的直观想法
计算机:是工具,帮助解决实际问题 操作系统,是为了方便使用硬件 计算机模型: 图灵机,模拟人类计算 起初的图灵机就像一个只会做一道菜的厨师 通用图灵机,核心是设置控制器动作(修改控制器),把逻辑读入控 ...
- 右手坐标系下LookAt视图矩阵的推导
基本知识 右手坐标系 右手手掌弯曲,手指方向由正X轴指向正Y轴,如果这时Z轴正方向与大拇指方向保持一致,坐标系为右手坐标系,否则为左手坐标系. 向量叉乘的方向 向量(1,0,0)与向量(0,1,0)叉 ...
- Vim 基本配置
1.关闭vi的一致性模式 set nocompatible 2.配置backspace的工作方式 set backspace=indent,eol,start 3.显示行号 set number 4. ...