c# 多维数组、交错数组(转化为DataTable)
2016-05-27 02:27
561 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; namespace ConsoleApplication31 { class Program { public static Array ReturnArray() { string[,,] arr = new string[2, 3, 4]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 4; k++) { arr[i, j, k] = i + "," + j + "," + k; } } } return arr; } private static Array GetJCSZ() { //string[][][] arr = new string[2][][]; //arr[0] = new string[2][]; //arr[0][0] = new string[3];//3 //arr[0][1] = new string[4];//4 //arr[0][0][0] = "0,0,0"; //arr[0][0][1] = "0,0,1"; //arr[0][0][2] = "0,0,2"; //arr[1] = new string[2][]; //arr[1][0] = new string[3]; //arr[1][1] = new string[4]; //arr[1][0][0] = "1,0,0"; //arr[1][0][1] = "1,0,1"; //arr[1][0][2] = "1,0,2"; //return arr; string[] arr = new string[3]; arr[0] = "0"; arr[2] = "2"; return arr; } public static void InitColumns(Array arr, ref Dictionary<string, DataColumn> dicCols, ref DataTable table) { for (int i = 0; i < arr.Length; i++) { if ((arr as dynamic)[i] is Array) { InitColumns((arr as dynamic)[i], ref dicCols, ref table); } else { if (arr.Length >= dicCols.Keys.Count) { dicCols.Clear(); for (int ii = 0; ii < arr.Length; ii++) { string colName = Guid.NewGuid().ToString(); DataColumn col = new DataColumn(colName); if (!dicCols.ContainsKey(colName)) { dicCols.Add(colName, col); } } } } } } public static DataTable ArrayConvert2DataTable(Array arr) { DataTable tmpT = new DataTable(); Dictionary<string, DataColumn> dicCols = new Dictionary<string, DataColumn>(); Dictionary<string, DataRow> dicRows = new Dictionary<string, DataRow>(); //J=交 C=错 bool isJC = !(arr.GetType().Name.Contains(',')); //交错数组处理 if (isJC) { //交错数组第一个维度的元素个 DataTable table = new DataTable(); List<int> dims = new List<int>(); InitColumns(arr, ref dicCols, ref table); foreach (var item in dicCols) { table.Columns.Add(item.Value); } int currRowIndex = 0; SearchTable(ref currRowIndex,arr,arr, ref table); return table; } //多维数组处理 else { int rank = arr.Rank; int cols = arr.GetLength(rank - 1); for (int i = 0; i < cols; i++) { DataColumn col = new DataColumn(Guid.NewGuid().ToString()); tmpT.Columns.Add(col); } Dictionary<int, int> dims = new Dictionary<int, int>(); int currRowIndex = -1; Dictionary<int, DataRow> dicRow = new Dictionary<int, DataRow>(); var iterator = arr.GetEnumerator(); int count = 0; while (iterator.MoveNext()) { var curr = iterator.Current; if (count % cols == 0) { currRowIndex++; DataRow dr = tmpT.NewRow(); tmpT.Rows.Add(dr); dicRow.Add(currRowIndex, dr); dr[0] = curr.ToString(); if (count == cols) { count = 0; } } else { tmpT.Rows[currRowIndex][count] = curr.ToString(); } count++; } } return tmpT; } private static void SearchTable(ref int currRowIndex, Array ori, Array curr, ref DataTable table) { for (int i = 0; i < curr.Length; i++) { bool isa = (curr as dynamic)[i] is Array; if (isa) { SearchTable(ref currRowIndex, ori, (curr as dynamic)[i], ref table); } else { if (table.Rows.Count < currRowIndex + 1) { DataRow newRow = table.NewRow(); table.Rows.Add(newRow); } try { table.Rows[currRowIndex][i] = (curr as Array).GetValue(i); } catch (Exception) { ; } if (i == curr.Length - 1) currRowIndex++; } } } static void Main(string[] args) { var t1 = ArrayConvert2DataTable(ReturnArray()); var t2 = ArrayConvert2DataTable(GetJCSZ()); Console.ReadKey(); } } }
相关文章推荐
- C# DataTable的詳細用法
- 改进C# WinForm窗体及其控件的自适应
- 一个C#写的开源分布式消息队列(类RocketMQ)
- C# Form窗体的功能操作,无边框窗体的移动,无边框窗体的尺寸缩放,保存和恢复窗体的尺寸和坐标信息
- C# 文件读取与保存
- C#6新特性,让你的代码更干净
- C# 从Resources资源中启动应用程序,应用程序简单加壳,导出Resources中的资源为文件
- Quartz.NET c# 教程 - 课程四:Triggers
- 《Head First C#》学习 ---第二章总结
- 字符串反转C#的实现
- C# 软件下载插件,软件自动更新功能实现,通过cmd命令调用应用程序,应用程序实现单例启动
- C#中的InitializeComponent()方法
- C# ThreadPool 线程同步
- C#函数式编程之递归调用
- C#函数式编程之序列
- C#函数式编程之可选值
- C#函数式编程之标准高阶函数
- 30天C#基础巩固------了解委托,string练习
- C# MVC4 QRCode二维码生成
- 一个输入指定盘符和文件名检索文件存在路径的c#程序