zoukankan      html  css  js  c++  java
  • c# 多维数组、交错数组(转化为DataTable)

    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();
            }
    
    
        }
    
    }
  • 相关阅读:
    [LeetCode]130 Surrounded Regions(DFS)
    [LeetCode]233 Number of Digit One(数位DP)
    POJ 3225.Help with Intervals-线段树(成段替换、区间异或、简单hash)
    POJ 2528.Mayor's posters-线段树(成段替换、离散数据、简单hash)
    POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)
    HDU 1698.Just a Hook-线段树(成段替换、输出总和tree[1])
    POJ 2886.Who Gets the Most Candies? -线段树(单点更新、类约瑟夫问题)
    计蒜客 28437.Big brother said the calculation-线段树+二分-当前第k个位置的数 ( ACM训练联盟周赛 M)
    HDU 5649.DZY Loves Sorting-线段树+二分-当前第k个位置的数
    计蒜客 28449.算个欧拉函数给大家助助兴-大数的因子个数 (HDU5649.DZY Loves Sorting) ( ACM训练联盟周赛 G)
  • 原文地址:https://www.cnblogs.com/kexb/p/5533279.html
Copyright © 2011-2022 走看看