c#中数组array和list在函数间传递 转置
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication2 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 double[,] array = new double[4, 2]; 13 for (int i = 0; i < 4; i++) 14 { 15 for (int j = 0; j < 2; j++) 16 { 17 array[i, j] = i + j; 18 } 19 } 20 21 //显示原数组 22 Console.WriteLine("Source Array:"); 23 for (int i = 0; i < 4; i++) 24 { 25 string soureResult = string.Empty; 26 for (int j = 0; j < 2; j++) 27 { 28 soureResult += array[i, j] + " "; 29 } 30 Console.WriteLine(soureResult); 31 } 32 33 double[,] newArray = Rotate(array); 34 //显示转置后的数组 35 Console.WriteLine("Destiney Array:"); 36 for (int i = 0; i < 2; i++) 37 { 38 string dstResult = string.Empty; 39 for (int j = 0; j < 4; j++) 40 { 41 dstResult += newArray[i, j] + " "; 42 } 43 Console.WriteLine(dstResult); 44 } 45 46 Console.ReadLine(); 47 } 48 49 public static double[,] Rotate(double[,] array) 50 { 51 int x = array.GetUpperBound(0); //一维 52 int y = array.GetUpperBound(1); //二维 53 double[,] newArray = new double[y + 1, x + 1]; //构造转置二维数组 54 for (int i = 0; i <= x; i++) 55 { 56 for (int j = 0; j <= y; j++) 57 { 58 newArray[j, i] = array[i, j]; 59 } 60 } 61 return newArray; 62 } 63 } 64 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication2 7 { 8 class Program 9 { 10 /// <summary> 11 /// 二维数组转置函数 12 /// </summary> 13 /// <param name="array"></param> 14 /// <returns></returns> 15 public static string[,] Rotate(string[,] array) 16 { 17 int x = array.GetUpperBound(0); //一维 18 int y = array.GetUpperBound(1); //二维 19 string[,] newArray = new string[y + 1, x + 1]; //构造转置二维数组 20 for (int i = 0; i <= x; i++) 21 { 22 for (int j = 0; j <= y; j++) 23 { 24 newArray[j, i] = array[i, j]; 25 } 26 } 27 return newArray; 28 } 29 30 /// <summary> 31 /// 将二维列表(List)转换成二维数组,二维数组转置,然后将二维数组转换成列表 32 /// </summary> 33 /// <param name="original"></param> 34 /// <returns></returns> 35 public static List<List<string>> Rotate(List<List<string>> original) 36 { 37 List<string>[] array = original.ToArray(); 38 List<List<string>> lists = new List<List<string>>(); 39 if (array.Length == 0) 40 { 41 throw new IndexOutOfRangeException("Index OutOf Range"); 42 } 43 int x = array[0].Count; 44 int y = original.Count; 45 46 //将列表抓换成数组 47 string[,] twoArray = new string[y, x]; 48 for (int i = 0; i < y; i++) 49 { 50 int j = 0; 51 foreach (string s in array[i]) 52 { 53 twoArray[i, j] = s; 54 j++; 55 } 56 } 57 58 string[,] newTwoArray = new string[x, y]; 59 newTwoArray = Rotate(twoArray);//转置 60 61 //二维数组转换成二维List集合 62 for (int i = 0; i < x; i++) 63 { 64 List<string> list = new List<string>(); 65 for (int j = 0; j < y; j++) 66 { 67 list.Add(newTwoArray[i, j]); 68 } 69 lists.Add(list); 70 } 71 return lists; 72 } 73 74 static void Main(string[] args) 75 { 76 List<List<string>> sourceList = new List<List<string>>(); //测试的二维List 77 for (int i = 0; i < 4; i++) 78 { 79 List<string> list = new List<string>(); 80 for (int j = 0; j < 2; j++) 81 { 82 list.Add(i.ToString() + j.ToString()); 83 } 84 sourceList.Add(list); 85 } 86 87 //显示原列表 88 Console.WriteLine("Source List:"); 89 for (int i = 0; i < sourceList.Count; i++) 90 { 91 string soureResult = string.Empty; 92 for (int j = 0; j < sourceList[i].Count; j++) 93 { 94 soureResult += sourceList[i][j] + " "; 95 } 96 Console.WriteLine(soureResult); 97 } 98 99 List<List<string>> dstList = Rotate(sourceList); 100 //显示转置后的列表 101 Console.WriteLine("Destiney List:"); 102 for (int i = 0; i < dstList.Count; i++) 103 { 104 string dstResult = string.Empty; 105 for (int j = 0; j < dstList[i].Count; j++) 106 { 107 dstResult += dstList[i][j] + " "; 108 } 109 Console.WriteLine(dstResult); 110 } 111 112 Console.ReadLine(); 113 } 114 } 115 }
参考:https://www.cnblogs.com/jeffwongishandsome/archive/2009/11/15/1603130.html
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication2 7 { 8 class Program 9 { 10 /// <summary> 11 /// 二维数组转置函数 12 /// </summary> 13 /// <param name="array"></param> 14 /// <returns></returns> 15 static void Main(string[] args) 16 { 17 List<List<double>> twolist = new List<List<double>>(); 18 List<double> onelist = new List<double>(); 19 onelist.Add(11.0); 20 onelist.Add(12.0); 21 onelist.Add(13.0); 22 twolist.Add(onelist); 23 onelist[0] = 21.0;//这样会将上面的11也抹去,可见这是地址引用 24 onelist[1] = 22.0; 25 onelist[2] = 23.0; 26 twolist.Add(onelist); 27 28 //onelist .AddRange (new List<double> {21.0,22.0,23.0}); 29 //twolist.AddRange(onelist); 30 double[,] two = twoDimenListToArray(twolist); 31 32 } 33 public static double[,] twoDimenListToArray(List<List<double>> twoDimenList) 34 { 35 36 List<double>[] array = twoDimenList.ToArray();//将twoDimenList转换为一维list,list元素为数组 37 if (array.Length == 0)//array这个一维list(元素为数组)有几个数组元素 38 { 39 throw new IndexOutOfRangeException("Index OutOf Range"); 40 } 41 int x = array[0].Count;//array这个一维list(元素为数组)第一个元素(数组)内有几个元素(double类型数据),相当于列数 42 int y = twoDimenList.Count;//二维list中有几个list,相当于行数 43 double[,] twoDimenArray = new double[y, x]; 44 for (int i = 0; i < y; i++)//先写行 45 { 46 int j = 0; 47 foreach (double d in array[i]) 48 { 49 twoDimenArray[i, j] = d; 50 j++; 51 } 52 } 53 return twoDimenArray; 54 } 55 56 } 57 }
修改:
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 == 0)//array这个一维list(元素为数组)有几个数组元素 { throw new IndexOutOfRangeException("Index OutOf Range"); } int x = array[0].Count;//array这个一维list(元素为数组)第一个元素(数组)内有几个元素(double类型数据),相当于列数 int y = twoDimenList.Count;//二维list中有几个list,相当于行数 double[,] twoDimenArray = new double[y, x]; for (int i = 0; i < y; i++)//先写行 { int j = 0; foreach (double d in array[i]) { twoDimenArray[i, j] = d; j++; } } return twoDimenArray; } } }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication2 7 { 8 class Program 9 { 10 /// <summary> 11 /// 二维数组转置函数 12 /// </summary> 13 /// <param name="array"></param> 14 /// <returns></returns> 15 static void Main(string[] args) 16 { 17 List<List<double>> twolist = new List<List<double>>(); 18 List<double> onelist = new List<double>(); 19 onelist.Add(11.0); 20 onelist.Add(12.0); 21 onelist.Add(13.0); 22 twolist.Add(onelist); 23 List<double> onelist1 = new List<double>(); 24 onelist1.Add(21.0);//这样会将上面的11也抹去,可见这是地址引用 25 onelist1.Add(22.0) ; 26 onelist1.Add(23.0); 27 twolist.Add(onelist1); 28 29 //onelist .AddRange (new List<double> {21.0,22.0,23.0}); 30 //twolist.AddRange(onelist); 31 double[,] two = twoDimenListToArray(twolist); 32 double[] one=oneDimenListToArray(onelist); 33 34 } 35 /// <summary> 36 /// 将二维list转换为二维数组 37 /// </summary> 38 /// <param name="twoDimenList">传入的要转换的二维list列表</param> 39 /// <returns>返回转换得到的二维数组</returns> 40 public static double[,] twoDimenListToArray(List<List<double>> twoDimenList) 41 { 42 43 List<double>[] array = twoDimenList.ToArray();//将twoDimenList转换为一维list,list元素为数组 44 if (array.Length == 0)//array这个一维list(元素为数组)有几个数组元素 45 { 46 throw new IndexOutOfRangeException("Index OutOf Range"); 47 } 48 int x = array[0].Count;//array这个一维list(元素为数组)第一个元素(数组)内有几个元素(double类型数据),相当于列数 49 int y = twoDimenList.Count;//二维list中有几个list,相当于行数 50 double[,] twoDimenArray = new double[y, x]; 51 for (int i = 0; i < y; i++)//先写行 52 { 53 int j = 0; 54 foreach (double d in array[i]) 55 { 56 twoDimenArray[i, j] = d; 57 j++; 58 } 59 } 60 return twoDimenArray; 61 } 62 /// <summary> 63 /// 将一维list转换为一维数组 64 /// </summary> 65 /// <param name="oneDimenList">传入的要转换的一维list列表</param> 66 /// <returns>返回转换得到的一维数组</returns> 67 public static double[] oneDimenListToArray(List<double> oneDimenList) 68 { 69 double[] oneDimenArray = oneDimenList.ToArray(); 70 return oneDimenArray; 71 } 72 73 } 74 }