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();
        }

    }

}
时间: 2024-10-25 06:24:32

c# 多维数组、交错数组(转化为DataTable)的相关文章

基本语法和数组(二维,多维,交错数组)

class Program { static void Main(string[] args) { // TestJiaoCuo(); Console.ReadKey(); } //交错数组. static void TestJiaoCuo() { //交错数组. 交错数组的本质是1个1维数组 只不过这个1维数组的元素又是数组,. int[][] arr = new int[3][]; arr[0] = new int[3]; arr[1] = new int[5]; arr[2] = new

C#中的多维数组和交错数组

C#中有多维数组和交错数组,两者有什么区别呢! 直白些,多维数组每一行都是固定的,交错数组的每一行可以有不同的大小. 以二维的举例,二维数组就是m×n的矩阵,m行n列:而交错数组(又叫锯齿数组)有m行,但是每一行不一定是n列.Got it? 还有要注意C#中的数组也是一种类型(C++中不是)! 下面看实例: 二维数组: public static void Main() { int row = 5; int column = 5; int[,] matrix = new int[row, col

二维数组与交错数组的理解

1:数组: //二维数组 表示1个表格. //交错数组的本质是1个1维数组//行固定 但是每1行的列数不固定 //string[][] arr = new string[3][]; //arr[0] = new string[4]; int[][] arr = { new int[] { 1, 2, 3 }, new int[] { 1, 2, 3, 4, 5, 5 } }; // int[] arr = { 1,2,3,4,5}; //行列都固定的情况下 使用二维数组. // string[,

POJ2155 Matrix 【二维树状数组】+【段更新点查询】

Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17766   Accepted: 6674 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1

poj 1195:Mobile phones(二维树状数组,矩阵求和)

Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The

HLJU 1188 Matrix (二维树状数组)

Matrix Time Limit: 4 Sec  Memory Limit: 128 MB Description 给定一个1000*1000的二维矩阵,初始矩阵中每一个数都为1,然后为矩阵有4种操作. S x1 y1 x2 y2:计算(x1,y1).(x2,y2)围成的矩阵内全部元素的和. A x y v:将(x,y)添加v D x y v:将(x,y)降低v M x1 y1 x2 y2 v:将(x1,y1)元素中的v转移到(x2,y2)中去. 全部操作数都为正数. 若某一操作将矩阵中元素降

POJ 1195-Mobile phones(二维树状数组-区间更新区间查询)

Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 17661   Accepted: 8173 Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The

poj1195(二维树状数组)

题目链接:https://vjudge.net/problem/POJ-1195 题意:有s*s的矩阵,初始化为全0,有两种操作,单点修改(x,y)的值,区间查询(x,y)的值(l<=x<=r,b<=y<=t). 思路:二维树状数组裸应用查询区间(l,b)~(r,t)的值可转换为tr[r][t]-tr[l-1][t]-tr[r][b-1]+tr[l-1][b-1].要注意的是输入的x,y是从0开始的,所以要加1. AC代码: #include<cstdio> #incl

【二维树状数组】See you~

https://www.bnuoj.com/v3/contest_show.php?cid=9148#problem/F [题意] 给定一个矩阵,每个格子的初始值为1.现在可以对矩阵有四种操作: A x y n1 :给格点(x,y)的值加n1 D x y n1: 给格点(x,y)的值减n1,如果现在格点的值不够n1,把格点置0 M x1 y1 x2 y2:(x1,y1)移动给(x2,y2)n1个 S x1 y1 x2 y2 查询子矩阵的和 [思路] 当然是二维树状数组 但是一定要注意:lowbi